2008-10-27 37 views
2

在Silverlight應用程序中,我試圖找出usercontrol上的屬性何時發生更改。我對一個特定的DependencyProperty感興趣,但不幸的是,控制本身並沒有實現INotifyPropertyChanged。有依賴項屬性發生更改時的通知機制嗎?

是否有任何其他方式來確定值是否已更改?

回答

1

隨着喬恩·加洛韋張貼在另一個線程,你也許能夠使用類似的WeakReference包你感興趣的屬性和在自己的類重新註冊。這是WPF代碼,但概念不依賴於DependencyPropertyDescriptor。

Article link

5

你可以在Silverlight中的問題。至少我做過。仍然需要看到利弊。

/// Listen for change of the dependency property 
    public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback) 
    { 

     //Bind to a depedency property 
     Binding b = new Binding(propertyName) { Source = element }; 
     var prop = System.Windows.DependencyProperty.RegisterAttached(
      "ListenAttached"+propertyName, 
      typeof(object), 
      typeof(UserControl), 
      new System.Windows.PropertyMetadata(callback)); 

     element.SetBinding(prop, b); 
    } 

現在,您可以調用RegisterForNotification來註冊元素屬性的更改通知,如。

RegisterForNotification("Text", this.txtMain,(d,e)=>MessageBox.Show("Text changed")); 
      RegisterForNotification("Value", this.sliderMain, (d, e) => MessageBox.Show("Value changed")); 

見我的崗位在這裏同http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html

+0

你是男人!在我找到你的答案之前,我已經掙扎了好幾個小時。 – 2010-08-28 23:56:17

相關問題