Home > Dependency Property, WPF > Shared Dependency Property

Shared Dependency Property

Dependency property defined in one class can be shared between different classes e.g. TextBlock.FontFamily and Control.FontFamily share the dependency property defined in the TextElement class.

Dependency property can be shared with other classes by calling a ‘AddOwner’ method on dependency property  which is being shared as shown below ;

TextBlock.FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(typeof(TextBlock));

AddOwner method also has one overload which accepts the FrameWorkPropertyMetadata object. So using this metadeta object property metadata can be specified.

However, sharing dependency property can be misleading sometimes. Following example describes that scenario :

Class T1 derives from Textbox having dependency property ‘IntTestProperty"’ defined in it. Class T2 shares the “IntTestProperty” implementation with the class T1.

In xaml code, I have defined style where I’m setting the dependency property “IntTest” values for both T1 & T2 types.

code behind file :

   // This class actually defines the Dependency property

     public class T1 : TextBlock
    {

       // Definition of Dependency property
        public static readonly DependencyProperty IntTestProperty = DependencyProperty.Register("IntTest", typeof(int), typeof(T1));

        public int IntTest
        {
            get { return (int)GetValue(IntTestProperty); }
            set { SetValue(IntTestProperty, value); }
        }
    }

    // definition of class which uses the dependency property defined above
    public class T2 : TextBlock
    {
        public static readonly DependencyProperty IntTestProperty = T1.IntTestProperty.AddOwner(typeof(T2));
        public int IntTest
        {
            get { return (int)GetValue(IntTestProperty); }
            set { SetValue(IntTestProperty, value);}
        }

}

Xaml code :

// style definition to apply on object

<Style x:Key="TestStyle">
    <Setter Property="dc:T1.IntTest" Value="5"/>
    <Setter Property="dc:T2.IntTest" Value="10"/>
</Style>

     <Canvas Left="300" Style="{StaticResource BigFontStyle}">
            <dc:T1 x:Name="t1"  Style="{StaticResource TestStyle}"/>
            <dc:T2 x:Name="t2"  Style="{StaticResource TestStyle}"/>
        </Canvas>

 

After initialization, if we read the value of the “IntTest” property for object t1 and t2 then value of both will be 10. As per the style definition the value of t1 object’s “IntTest” property should 5 and t2 object’s “IntTest” property should be 10. But because t1 and t2 share the same dependency property infrastructure, code sets the value of dependency property twice and when value is read for the property it returns the latest value set on it. That is why value is read as 10 after initialization with above code.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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: