WPF: The Static Nature of Dependency Properties

Posted by: the telerik blogs, on 25 Jul 2008 | View original | Bookmarked: 0 time(s)

With dependency properties being so heavily used across the WPF platform, there is a good chance that sooner or later you will get bitten by this issue my colleague Hristo Deshev and I were debugging not long ago. Some heads-up can't really hurt anybody so here it is;).

We will declare a simple class Foo with a single collection dependency property (note that this is a reference type). We will also supply our brand new property with a default value through the convenient DependecyProperty.Register(...) method:

staticDependencyProperties_foo1

Let us put our class to the test by creating two Foo instances and adding a single item to each Bars collection property:

staticDependencyProperties_foo2

Works as simple as that! But... I got a strange feeling about this -- let's run the debugger on the same piece of code just to make sure everything is OK.

The first Foo instance contains a single item in its Bar collection as expected:

staticDependencyProperties_foo3

 

However, the Bars content of the second instance might not be what one would expect:

staticDependencyProperties_foo4

As the excellent debugging utility "Make Object ID" tips us, it seems all distinct Foo object instances share the same Bars collection object -- thus when you add an item to a single Foo.Bars collection, you are essentially adding the item to the Bars collection of all Foo objects.

Most probably you are not trying to utilize this static feature of the WPF dependency properties so you can eliminate it by moving the default value declaration from the DependencyProperty.Register(...) call to the Foo constructor:

staticDependencyProperties_foo5

And here is the debugger once again (Make Object ID also states this is a separate object now {#2}):

staticDependencyProperties_foo6

 

Voila!

Category: Debugger | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 5879 | Hits: 92

Similar Posts

  • Animating the RadWindow control for Silverlight and WPF. more
  • Better XAML By Farr: WPF Line of Business Seminar more
  • Generic WPF Drag and Drop Adorner more
  • Animation Hack Using Attached Properties in Silverlight more
  • Framework Design Guidelines: Dependency Properties more
  • Putting the Silverlight Layout System to Work more
  • Debugging Dependency Properties in WPF: Part 3 more
  • Debugging Dependency Properties in WPF with Property Changed Callbacks (or: How I Learned to Stop Worrying and Love printf Debugging) more
  • Silverlight Dependency Properties more
  • TreeView in a ComboBox dropdown using RadControls for Silverlight more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD