0

我有一个小问题,找不到解决方法。我创建了一个带有命令绑定的按钮。此按钮调用 DelegateCommand,但我需要此按钮的“e.Tag”,而 DelegateCommand 只返回“null”。那么你们中有人知道解决这个问题的方法吗?附言。ImgSource ist 绑定到 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
    }
}
4

1 回答 1

0

你确定e.Source有必要吗?如果您在引发它的同一控件上处理该事件,则可以改为转换发送者。

于 2012-02-15T08:50:29.023 回答