我已经像这样实现了INotifyPropertyChanged接口,
private int total;
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public int Total {
get { return this.Total; }
set
{
if (this.total == value) return;
this.total = value;
this.NotifyPropertyChanged("TotalCost");
}
}我必须将public int TotalCost的值绑定到一个文本框中。每当其他文本框中的值发生变化时,TotalCost就会发生变化。我已经做了动态绑定,绑定
bind = new Binding();
bind.Source = this.DataContext; TotalText.SetBinding(TextBox.TextProperty, bind);并将该类的DataContext设置为TotalCost。我哪里错了?谢谢
发布于 2010-06-20 21:13:55
我认为NotifyPropertyChanged没有被触发的原因是因为属性名不匹配。公共属性的名称必须与传递给NotifyPropertyChanged方法的字符串相同。因此,不是调用:
this.NotifyPropertyChanged("TotalCost");你应该打电话给我:
this.NotifyPropertyChanged("Total"); 这应该可以解决这个问题。
发布于 2010-06-21 09:59:40
你的getter不应该是这样的吗?
get { return total;}
也许它正在被设置,但是getter没有返回它...
发布于 2010-06-20 17:47:11
private int _total=0;
public int Total
{
get
{
return this._total;
}
set {
if (this._total == value)
return;
this._total = value;
this.NotifyPropertyChanged("Total"); }
}
...
bind = new Binding("DataContext.Total");
bind.Source = this;
bind.Mode = BindingMode.TwoWay;
TotalText.SetBinding(TextBox.TextProperty, bind);
...
this.DataContext=this;https://stackoverflow.com/questions/3078775
复制相似问题