e.Tag DelegateCommand Binding

时间:2012-02-15 03:59:38

标签: c# wpf binding delegatecommand

我有一点问题,找不到如何解决它。我创建了一个带有Commandbinding的Button。这个Button调用一个DelegateCommand,但我需要这个按钮的“e.Tag”,而DelegateCommand只返回“null”。所以你们中有谁知道解决这个问题的方法吗? PS。 ImgSource被绑定到Imagesource所以我需要这种方式在运行时更改它。 Button本身有效..

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
    }
}

1 个答案:

答案 0 :(得分:0)

您确定e.Source是否必要?如果您在提升它的同一控件上处理事件,则可以转发发件人。