e.Tag DelegateCommand 绑定
我有一个小问题,找不到解决办法。我创建了一个带有命令绑定的按钮。此按钮调用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定
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.