If you’re writing a custom control in WPF, you may have encountered a very annoying bug: if you set the value of a DependencyProperty in your implementation code, the local value will trump any changes that you may try to apply to this property using bindings, styles, etc. You can read more about this in this post in Vincent Sibal’s blog.

WPF 4 introduces a solution for this problem: the DependencyObject.SetCurrentValue() method. It allows changing the value of a DP without affecting the BaseValueSource. Vincent has a nice post explaining this as well. As he says, a control author should generally use this method instead of SetValue().

However, if you’re still working on a .NET 3-3.5 app, and are encountering this issue, fear not! For there is a workaround, albeit slightly cumbersome. This magic lies within the power of coercion. Like the new SetCurrentValue(), the good old CoreceValue() method does not change the BaseValueSource. So, instead of setting the value directly, put some logic in the coercion callback of your DP. Your bindings and styles will continue to work after that.

I’ve changed the sample from Vincent’s post to use this workaround (take a look at the CustomControl.CoerceTitle() method).

Attachment: ControlLocalValuesSample.zip

Tagged with:
 

Comments are closed.

Set your Twitter account name in your settings to use the TwitterBar Section.