e.Tag DelegateCommand 绑定

发布于 2025-01-05 09:44:04 字数 1239 浏览 5 评论 0原文

我有一个小问题,找不到解决办法。我创建了一个带有命令绑定的按钮。此按钮调用 DelegateCommand,但我需要此按钮的“e.Tag”,而 DelegateCommand 仅返回“null”。那么你们中有人知道解决这个问题的方法吗? 附: ImgSource 绑定到图像源,所以我需要这种方式在运行时更改它。 按钮本身可以工作..

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

i have a little problem and find no way how to fix it. I create a Button with a Commandbinding. This Button calls a DelegateCommand, but i need the "e.Tag" of this button and DelegateCommand just return "null". So do any of you know a way to solve this?
ps. ImgSource ist bound to an Imagesource so i need this way to change it at runtime.
The Button itself works..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2025-01-12 09:44:04

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

Are you sure that e.Source is necessary? If you handle the event on the same control that raises it you could cast the sender instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文