我用一個Multibinding來綁定一些屬性,並使用INotifyPropertyChanged接口來通知這些屬性的變化。但令人遺憾的是,似乎INotifyPropertyChanged不起作用。 PropertyChangedEventHandler「PropertyChanged」始終爲空。爲什麼INotifyPropertyChanged不起作用?
問題:?
A.你能告訴我,爲什麼事件是空在我的印象中,應該是事件的默認方法PropertyChangedEventHandler,我錯了
?(已解決,謝謝!)B.就像一些朋友說,我再次嘗試不使用INotifyPropertyChanged.But目標的屬性值似乎沒有改變......
PROPERT IES
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(好像用轉換器用以計算由 「TopOffset」 和 「左偏移」 影響了地方工作well.By)
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");
你有依賴屬性,但是這個接口被應用於通過setter中的函數調用的公共屬性。 – vorrtex 2011-02-14 08:23:56
謝謝你,vorrtex.But原因可能不會像你說的。我試圖通知一個共同的屬性,事件仍然爲空... – Claw 2011-02-14 08:35:34
Vorrtex意味着在你的情況下,綁定引擎監聽依賴屬性的變化在WPF中。所以沒有PropertyChanged的處理程序。 – 2011-02-14 08:49:17