Local values in DependencyObjects
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
Tags
.NET 4 Animation AppFabric Async Axum Blog C# ClearType Cloud CLR CodeValue Contests Deep Zoom Experiments Generics Google Ink Lectures Modeling Performance Personal Pivot Prism Programming Languages Prolog Reflector RTL Sela Silverlight The Arbel Network Themes Threading Tips Visual Studio WCF Windows 7 Windows 2003 Windows Azure Windows Forms Windows Phone Windows Vista Windows XP WPF XAML ZuneArchives
- May 2013
- February 2013
- June 2012
- May 2012
- June 2011
- November 2010
- August 2010
- July 2010
- June 2010
- March 2010
- December 2009
- November 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- November 2007
- September 2007
- June 2007
- May 2007
- February 2007
- November 2006
- October 2006
- February 2006
- August 2005
- February 2005
- August 2004
- July 2004
- June 2004
- May 2004





