Posted by: Zeeshan Amjad | August 10, 2009

AddOwner functionality of Dependency Property in WPF


Sometime we may have the same dependency property at two different class and wants to share that functionality rather than duplicating the same code in each classes. In that case AddOwner is a handy function to share the dependency property among different classes. In real life this we may face this situation when we are creating our own control.

Lets take a look at one example. In we are going to create two user control with one dependency property in each. Just for the simplicity we created very simple user control with a border with some transformation applied to it.

Here is a code of our first user control.

  1: <UserControl x:Class="WpfUserControl.FirstControl"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Height="100" Width="100">
  5:     <Border Background="Wheat" Margin="25" Width="100" Height="100">
  6:         <Border.RenderTransform>
  7:             <RotateTransform Angle="45"/>            
  8:         </Border.RenderTransform>     
  9:     </Border>            
 10: </UserControl>
 11: 

 

The other user control is almost the same as the first one the only difference is we are going to use different transformation here. Here is the code for second user control.

  1: <UserControl x:Class="WpfUserControl.SecondControl"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Height="100" Width="100">
  5:     <Border Background="Wheat" Margin="25" Width="100" Height="100">
  6:         <Border.RenderTransform>
  7:             <SkewTransform CenterX="0" CenterY="0" AngleX="10" AngleY="10"/>
  8:         </Border.RenderTransform>
  9:     </Border>
 10: </UserControl>
 11: 

Here is the simple code to use both user controls in our program.

  1: <Window x:Class="WpfUserControl.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:local="clr-namespace:WpfUserControl"
  5:     Title="AddOwner Dependency Property" Height="300" Width="400">
  6: 
  7:     <StackPanel>
  8:         <local:FirstControl />
  9:         <local:SecondControl />
 10:     </StackPanel>
 11: </Window>
 12: 

Here is the output of this program.

AddOwner

Now lets take a look at the fun part. First we are going to introduce dependency property in our first control. Here is the code to introduce dependency property in the first user control.

  1: using System;
  2: using System.Windows;
  3: using System.Windows.Media;
  4: using System.Windows.Controls;
  5: 
  6: namespace WpfUserControl
  7: {
  8:     /// <summary>
  9:     /// Interaction logic for FirstControl.xaml
 10:     /// </summary>
 11:     public partial class FirstControl : UserControl
 12:     {
 13:         public static readonly DependencyProperty ControlColorProperty =
 14:             DependencyProperty.Register("ControlColor", typeof(Brush), typeof(FirstControl));
 15: 
 16:         public Brush ControlColor
 17:         {
 18:             get { return (Brush)GetValue(ControlColorProperty); }
 19:             set 
 20:             { 
 21:                 SetValue(ControlColorProperty, value); 
 22:                 this.border.Background = value; 
 23:             }
 24:         }
 25: 
 26:         public FirstControl()
 27:         {
 28:             InitializeComponent();
 29:         }
 30:     }
 31: }
 32: 

In the second control, instead of doing everything again, we just call the AddOwner function of dependency property.

  1:         public static readonly DependencyProperty ControlColorProperty =
  2:             DependencyProperty.Register("ControlColor", typeof(Brush), typeof(FirstControl));
  3: 

Now we will have same dependency property in both user controls. here is the complete coding of our second user control.

  1: using System;
  2: using System.Windows;
  3: using System.Windows.Media;
  4: using System.Windows.Controls;
  5: 
  6: namespace WpfUserControl
  7: {
  8:     /// <summary>
  9:     /// Interaction logic for SecondControl.xaml
 10:     /// </summary>
 11:     public partial class SecondControl : UserControl
 12:     {
 13:         public static readonly DependencyProperty ControlColorProperty =
 14:             FirstControl.ControlColorProperty.AddOwner(typeof(SecondControl));
 15: 
 16:         public Brush ControlColor
 17:         {
 18:             get { return (Brush)GetValue(ControlColorProperty); }
 19:             set 
 20:             { 
 21:                 SetValue(ControlColorProperty, value); 
 22:                 this.border.Background = value; 
 23:             }
 24:         }
 25: 
 26:         public SecondControl()
 27:         {
 28:             InitializeComponent();
 29:         }
 30:     }
 31: }
 32: 
Advertisements

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

Categories

%d bloggers like this: