Why doesn't the INotifyPropertyChanged work?
I used a Multibinding to bind some properties and use the INotifyPropertyChanged interface to notify these properties'changes.But sadly,it seems that the INotifyPropertyChanged didn't work. The PropertyChangedEventHandler "PropertyChanged" was null all the time.
Questions:
A. Could you please tell me why the event is null?In my mind,there should be a default method for the event PropertyChangedEventHandler,am I wrong?(Resolved,thanks!)
B. Just like some friends said,I tried again without using the INotifyPropertyChanged.But the target's property's value seems to be not changed...
Properties
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); } }
Multibinding (It seems work well.By using the converter to calculate a location which is affected by "TopOffset" and "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, multibi开发者_运维问答nding);
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)); }
Notify the change
SetValue(LeftOffsetProperty, moveAdorner.LeftOffset); CallPropertyChanged("LeftOffset"); SetValue(TopOffsetProperty, moveAdorner.TopOffset); CallPropertyChanged("TopOffset");
You don't need to implement INotifyPropertyChanged for dependency properties. Binding will track changes of these properties automatically.
Set Mode=TwoWay
anywhere in your binding ?
You need to override OnPropertyChanged and check to see if the property is the one you are looking for.
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
if (e.Property == MyProperty)
{
// do something
}
base.OnPropertyChanged(e);
}
精彩评论