在Silverlight應用程序中,我試圖找出usercontrol上的屬性何時發生更改。我對一個特定的DependencyProperty感興趣,但不幸的是,控制本身並沒有實現INotifyPropertyChanged。有依賴項屬性發生更改時的通知機制嗎?
是否有任何其他方式來確定值是否已更改?
在Silverlight應用程序中,我試圖找出usercontrol上的屬性何時發生更改。我對一個特定的DependencyProperty感興趣,但不幸的是,控制本身並沒有實現INotifyPropertyChanged。有依賴項屬性發生更改時的通知機制嗎?
是否有任何其他方式來確定值是否已更改?
在WPF中你有DependencyPropertyDescriptor.AddValueChanged,但不幸的是在Silverlight中沒有這樣的事情。所以答案是否定的。
也許如果你解釋你想做什麼,你可以解決這個問題,或者使用綁定。
隨着喬恩·加洛韋張貼在另一個線程,你也許能夠使用類似的WeakReference包你感興趣的屬性和在自己的類重新註冊。這是WPF代碼,但概念不依賴於DependencyPropertyDescriptor。
請查看以下鏈接。它showns怎麼去解決,你不具備DependencyPropertyDescriptor.AddValueChanged
http://themechanicalbride.blogspot.com/2008/10/building-observable-model-in.html
你可以在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
你是男人!在我找到你的答案之前,我已經掙扎了好幾個小時。 – 2010-08-28 23:56:17