Is there a notification mechanism when the dependency property has changed?

In a Silverlight application, I am trying to figure out when a property has changed in usercontrol. I am interested in one particular DependencyProperty, but unfortunately the control itself does not implement INotifyPropertyChanged.

Is there another way to determine if a value has changed?

+2
source share
4 answers

In WPF, you are DependencyPropertyDescriptor.AddValueChanged , but unfortunately there is no such thing in Silverlight. So the answer is no.

Perhaps if you explain what you are trying to do, you can work around the situation or use bindings.

+2
source

You can. At least I did it. You still need to see the pros and cons.

/// 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); } 

And now you can call RegisterForNotification to register to notify you of an element property change, for example.

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

See my post here on the same http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html

+5
source

As John Galloway said in another thread, you can use something like WeakReference to wrap properties you are interested in and re-register them in your class. This is WPF code, but the concept is independent of DependencyPropertyDescriptor.

Article Link

+1
source

Check out the following link. It showed how to get around the problem in silverlight where you don't have DependencyPropertyDescriptor.AddValueChanged

http://themechanicalbride.blogspot.com/2008/10/building-observable-model-in.html

0
source

All Articles