#147 – Use SetCurrentValue When You Want to Set A Dependency Property Value from Within a Control
December 6, 2010 3 Comments
As a control author, you might want to set the value of one of your own dependency properties. For example, a numeric up/down control might want to change its Value property in response to clicking on the arrows.
The problem with setting the dependency property directly is that you’ve set a local value, which will take precedence over all other possible sources, like data binding. If you set a local value, you’ll destroy any data binding that the application has set up.
The solution is to use the DependencyObject.SetCurrentValue method to set the current value from within the control. This is similar to coercion, in that the effective value is changed without affecting the property’s value source.
A control should always use SetCurrentValue to set the value of its own dependency properties, with the exception of the CLR property setter, which should use SetValue.
See also Vincent Sibal’s blog.