Alex Rivera | Logout

Listen to changes of dependency property

Asked 2011-01-21T23:25:39.023
89

Is there any way to listen to changes of a DependencyProperty? I want to be notified and perform some actions when the value changes but I cannot use binding. It is a DependencyProperty of another class.

Edit
Report

2 Answers

164

This method is definitely missing here:

DependencyPropertyDescriptor
    .FromProperty(RadioButton.IsCheckedProperty, typeof(RadioButton))
    .AddValueChanged(radioButton, (s,e) => { /* ... */ });

Caution: Because DependencyPropertyDescriptor has a static list of all handlers in application every object referenced in those handlers will leak if the handler is not eventually removed. (It does not work like common events on instance objects.)

Always remove a handler again using descriptor.RemoveValueChanged(...).

answered 2011-08-09T23:58:09.917
1

If that is the case, One hack. You could introduce a Static class with a DependencyProperty. You source class also binds to that dp and your destination class also binds to the DP.

answered 2011-01-22T09:08:09.503

Your Answer