我使用了Multibinding来绑定一些属性,并使用INotifyPropertyChanged接口通知这些属性的更改。但是可悲的是,似乎INotifyPropertyChanged无法正常工作。 PropertyChangedEventHandler“ PropertyChanged”始终为null。
问题:
答:能否请您告诉我为什么该事件为null?在我看来,应该为该事件设置一个默认方法PropertyChangedEventHandler,我错了吗?(已解决,谢谢!)
B.就像一些朋友说的那样,我没有使用INotifyPropertyChanged再次尝试。但是目标的属性值似乎没有改变。
物产
public static readonly DependencyProperty LeftOffsetProperty = DependencyProperty.Register("LeftOffset", typeof(double), typeof(NetworkTaskLable), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty TopOffsetProperty = DependencyProperty.Register("TopOffset", typeof(double), typeof(NetworkTaskLable), new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.AffectsRender));
public double LeftOffset
{
get { return (double)GetValue(LeftOffsetProperty); }
set { SetValue(LeftOffsetProperty, value); }
}
public double TopOffset
{
get { return (double)GetValue(TopOffsetProperty); }
set { SetValue(TopOffsetProperty, value); }
}
多重绑定(似乎效果很好。通过使用转换器来计算受“ TopOffset”和“ LeftOffset”影响的位置)
var multibinding = new MultiBinding() { Converter = new BeginAndStartDateToLeftConverter_NetworkTaskLable(), ConverterParameter = NetworkView };
multibinding.Bindings.Add(new Binding("Start"));
multibinding.Bindings.Add(new Binding("StartDate") { Source = NetworkView });
multibinding.Bindings.Add(new Binding("LeftOffset") { Source = this });
MainCanvas.SetBinding(LeftProperty, multibinding);
INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void CallPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)//It seems to be null all the time!!!And the properties'changes were never notified!!!
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
通知变更
SetValue(LeftOffsetProperty, moveAdorner.LeftOffset);
CallPropertyChanged("LeftOffset");
SetValue(TopOffsetProperty, moveAdorner.TopOffset);
CallPropertyChanged("TopOffset");
最佳答案
您不需要为依赖项属性实现INotifyPropertyChanged。绑定将自动跟踪这些属性的更改。
关于c# - 为什么INotifyPropertyChanged不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4989899/