WPF数据绑定不起作用
这是我从字符串(History.current_commad)到文本框(tbCommand)的数据绑定: history.current_命令正在更改,但文本框未更新。怎么了WPF数据绑定不起作用,wpf,data-binding,textbox,Wpf,Data Binding,Textbox,这是我从字符串(History.current_commad)到文本框(tbCommand)的数据绑定: history.current_命令正在更改,但文本框未更新。怎么了 谢谢您之所以看不到文本块中反映的更改是因为当前_命令只是一个字段,因此绑定不知道它是何时被执行的 解决此问题的最简单方法是让历史类实现INotifyPropertyChanged,将当前_命令转换为属性,然后在属性的setter中引发PropertyChanged事件: public class History : INo
谢谢您之所以看不到
文本块中反映的更改
是因为当前_命令
只是一个字段,因此绑定
不知道它是何时被执行的
解决此问题的最简单方法是让历史
类实现INotifyPropertyChanged
,将当前_命令
转换为属性,然后在属性的setter中引发PropertyChanged
事件:
public class History : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
private string _current_command;
public string current_command
{
get
{
return _current_command;
}
set
{
if (_current_command == null || !_current_command.Equals(value))
{
// Change the value and notify that the property has changed
_current_command = value;
NotifyPropertyChanged("current_command");
}
}
}
}
现在,当您为当前_命令
赋值时,事件将触发,绑定
也将知道更新其目标
如果您发现自己有很多类要绑定到它们的属性,则应该考虑将事件和帮助器方法移到基类中,这样就不会重复编写相同的代码。
<代码>历史类看起来是什么?公共类历史{Prpublic String Currnth-Cub命令;} Hi@ DLV,你能看看这个问题吗?我在代码中进行数据绑定的原因是我无法正确地使用xmlns。谢谢public class History : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
private string _current_command;
public string current_command
{
get
{
return _current_command;
}
set
{
if (_current_command == null || !_current_command.Equals(value))
{
// Change the value and notify that the property has changed
_current_command = value;
NotifyPropertyChanged("current_command");
}
}
}
}