The CLR, Dependency Properties, Routed Events and Cider

Dependency Properties and Routed Events are special WPF mechanisms that can be used in place of the CLR’s normal Properties and Events, since they are more powerful, although less typed. You can even define a dependency property or routed event without a matching property or event and one can access the property’s value or attach to the event using simple code.

However, it seems that what you can do in code, you can’t do in XAML. For instance, I have a control named Test, that has a property named MyProperty. If I decide against exposing it as a CLR property (maybe it only has meaning when used in WPF’s markup), I can still use this line to set it:

this.test.SetValue(Test.MyPropertyProperty, 10);

The above line compiles, runs and I’m a happy camper. However, try to get the following XAML to compile:

<local:Test x:Name="test" MyProperty="10" />

You’ll get: error MC3072: The property ‘MyProperty’ does not exist in XML namespace […]

So what this means is that without CLR properties exposing them, you can’t use dependency properties in XAML (the same is true for events and routed events).

It strikes me as odd that WPF’s XAML compiler can’t handle its own mechanism without requiring you to add more, unnecessary code.

Advertisements

3 thoughts on “The CLR, Dependency Properties, Routed Events and Cider

  1. I should add that I believe it does require you to add at least a static setter method that goes with your definition of your attached property. E.g.:
    class Test : UIElement {
    public static readonly DependencyProperty MyPropertyProperty = …Register(“MyProperty”, …);
    public static void SetMyProperty(UIElement target, string value) {
    target.SetValue(MyPropertyProperty, value);
    }
    }

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