Want to use Dynamic Dependency Properties in WPF XAML?

in German: "Dynamische Semmel zum Frühstück gefällig?"

by Nikolay Nikolov

Let's first mention the motivation:

I wanted to allow the following XAML fragment

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:me="clr-namespace:DynamicDependencyProperty;assembly=DynamicDependencyProperty40"
>
 <me:DPDefinitions.DynamicDefinitions>
   <me:DependencyPropertyDefinition Type="{x:Type x:String}" Name="Try" DefaultValue="Created by XAML!" />
 </me:DPDefinitions.DynamicDefinitions>

 <Grid Name="g" me:DPDefinitions.Try="Setting value in XAML &amp; DataBinding works!" >
   <TextBlock Text="{Binding Path=(me:DPDefinitions.Try), ElementName=g}"/>
 </Grid>
</Window>

OK, that's the XAML 4.0 syntax as you can see, but with small modifications it (should) run also in XAML 3.5.

For now I want it allowed in .NET 4.0 where having XamlSchemaContext offers so great possibilities. (at least on first glance, but about that later...)

I've marked above the important parts with bold font:

1. The definition of the (always attached) DProperty DPDefinitions.Try of type String with a default value. One can imagine some enhancements to this syntax - e.g. the owner type of all dynamic DPs, internally defined as the type DPDefinitions could also be set by a TypeExtension. That's not of importance for this test.

But most important: We omit any kind of code for the attached DP - why bother with CLR getter and setter when we have tons of Get/SetValue methods spread in DependencyObject, DependencyPropertyDescriptor etc. ...

2. Setting the value on the grid element. The syntax of setting is the same as for code-based (non-dynamic) DPs

3. Using DataBinding together with a path containing the just defined dynamic DP

And now comes the big Question (That s why the question mark at the end of the title)

CAN THIS BE ALLOWED IN .NET 4.0 without using reflection, internal members etc.?

I'll leave the question open. I wanted to talk with Rob Relyea on that.

Another thing is: Can the new Attachable properties be used for that?

CU

Comments

# Twitter Trackbacks for Dynamische Semmel zum Fr??hst??ck gef??llig? - XAML Blog [codecomplete.de] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Dynamische Semmel zum Fr??hst??ck gef??llig? - XAML Blog         [codecomplete.de]        on Topsy.com

Mittwoch, 14. April 2010 11:51

Leave a Comment

(required) 
(required) 
(optional)
(required)