Skip navigation

First things first. I’d like to wish everyone a Happy New Year. Here’s hoping that 2008 brings with it happiness, excitement, joy, and wonder!

Recently I came across a blog post that discussed the use of Custom Type Descriptors to dynamically add properties to an object. We currently use this technique in our application, however there is a lot of hocus pocus going on behind the scenes that makes working with that particular piece of code precarious.

I was curious if it would be possible to dynamically declare dependency properties and bind to them using the WPF Binding system. The short answer is yes. The long answer is HELL YES! Believe me using dependency properties to dynamically add properties to your objects is orders of magnitude easier than using custom type descriptors. Something tells me that DPs are just an abstraction of custom type descriptors. Does that make me lazy? Probably. Then again, the less code I have to write to accomplish a task, the less likely I am to introduce bugs, and the quicker I can get to the next task. And as we all know, "programming  is fun, but shipping is our job." Besides, you want to trust me on this one…programming Custom Type Descriptors is not fun.

Of course you, my dear readers, are probably wondering, "Why would I want to dynamically register dependency properties?" Isn’t the whole point of a dependency property to allow you to bind to it at design time? Well not only does Mabsterama give a great scenario with the role provider, but there is a well known data modeling pattern known as Entity Attribute Value. I just ran into a post in the forums (which are finally working properly again) where the poster appears to be in need of a similar solution. This encouraged me to finish this post.

My solution relies on a simple class that I call the DependencyPropertyRegistry. The DependencyPropertyRegistry is responsible for dynamically registering dependency properties against a DependencyObject descendant and keeping track of the properties so they can be manipulated. The second part of the solution is a class that wants to dynamically expose properties for binding. The final piece of the puzzle is a control that knows how to properly configure the bindings and display them.

This is easiest explained through code, but I have to do some scrubbing before I can share it with you guys. I promise it will be worth the wait.

Advertisements

8 Comments

  1. You\’re doin a helluva job. Looking forward to the code. 

  2. If you post what you got so far, we\’ll give you a hand with the "scrubbing" 

  3. By "scrubbing" I meant removing code that was not approved for public consumption by my employer. I have a few personal priorities that a precluding that right now.

  4. What are your plans for sharing the code.  I am interested in a working scrubbed example but if that is not possible, then some code snippets with explaination would be appreciated.
     
    Thanks.

  5. Mark,
       You just moved this to the top of my list. I\’ll get an example ready this weekend.

  6. What led you to choose EAV?

    I once wasted a week rewriting a perfectly good application to use an
    EAV data model, and then realized my mistake and threw out the
    EAV-referencing code. In my experience, EAV is easy to set-up, but
    going forward is exponentially painful…

    Can we finally see your TypeDescriptorProvider tutorial?  You recently mentioned it on the WPF Disciples mailing list, which reminded me aout this blog post…

  7. And where is code?

  8. And where is the code? 🙂 I tried to do it myself and everything worked just fine until typeof(dynamic) part in registration of the DependencyProperty.

    Also, I wasn’t able to use dynamic as an output parameter type, and that was the thing that truly destroyed the whole idea.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: