e.标记DelegateCommand绑定

本文关键字:绑定 DelegateCommand 标记 | 更新日期: 2023-09-27 18:24:31

我遇到了一个小问题,无法解决。我创建了一个带有Commandbinding的Button。这个按钮调用DelegateCommand,但我需要这个按钮的"e.Tag",DelegateCommand只返回"null"。你们知道解决这个问题的方法吗?ps.ImgSource绑定到一个Imagesource,所以我需要这种方式在运行时更改它。按钮本身工作。。

public Datenbank datab = new Datenbank();
Binding b = new Binding("GetValue");
b.Source = datab;
champbtn.SetBinding(Button.CommandProperty, b);
champbtn.Tag = path;
public class Datenbank : INotifyPropertyChanged
{
    private string _sourcep;
    public string ImgSource
    {
        get { return _sourcep; }
        set
        {
            _sourcep = value;
            NotifyPropertyChanged("ImgSource");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyname)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
    }
    public Datenbank()
    {
        GetValue = new DelegateCommand(Set);
    }
    public void Set(object sender, RoutedEventArgs e)
    {
        System.Windows.Controls.Button src = e.Source as System.Windows.Controls.Button;
        string taged = src.Tag.ToString();
        ImgSource = taged;
        //This causes an error because e == null
    }
}

e.标记DelegateCommand绑定

您确定e.Source是必需的吗?如果在引发该事件的同一控件上处理该事件,则可以强制转换发送方。