(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

Tip: How to declare an attached property in Silverlight?

(23 votes)
Ilia Iordanov
>
Ilia Iordanov
Joined Oct 25, 2007
Articles:   12
Comments:   89
More Articles
2 comments   /   posted on Sep 10, 2008
Categories:   General

Probably the first most important thing to mention here is that only inheritors of System.Windows.DependencyObject can be extended with attached properties. This is needed because either to set or to get value for a specific attached property, you need to use the methods SetValue and GetValue which are defined in the DependencyObject class.
When you declare an attached property, many advantages from the dependency properties model are coming out of the box for you such as caching, data binding, default values, expressions, styling, property invalidation and more.

In order to declare a attached property in Silverlight, you have to follow few simple steps as explained in the sample code below.

C#

public partial class MySilverlightControl : StackPanel
{
    //1. Declare the attached property as static, readonly field in your class.
   public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached(
              "MyProperty",                                 //Property name
              typeof( string ),                             //Property type
              typeof( MySilverlightControl ),               //Type of the dependency property provider
              new PropertyMetadata( MyPropertyChanged ) )//Callback invoked on property value change
 
    public MySilverlightControl()
    {
        InitializeComponent();
    }
 
    //2. Define the appropriate SetXXX and GetXXX methods, 
    //where XXX should be replaced with the property name
    public static void SetMyProperty( DependencyObject obj, string propertyValue )
    {
        obj.SetValue( MyPropertyProperty, propertyValue );
    }
    public static string GetMyProperty( DependencyObject obj )
    {
        return ( string )obj.GetValue( MyPropertyProperty );
    }
 
    private static void MyPropertyChanged( object sender, DependencyPropertyChangedEventArgs args )
    {
        //Do some processing here when the attached property value has changed...
    }
 
    //Just a sample method illustrating the idea how to obtain the value
    //of the MyProperty property for a specific element.
    private void ProcessTabKey()
    {
        foreach ( UIElement element in this.Children )
        {
            string propertyValue = MySilverlightControl.GetMyProperty( element );
            //Perform some processing according to the my property value.............
        }
    }
}

Read more about DependencyObject and DependencyProperty classes on MSDN.
If you need more information about the attached properties check out this article 'Attached properties in Silverlight'.

That's it!


Subscribe

Comments

  • -_-

    RE: Tip: How to declare an attached property in Silverlight?


    posted by Maddy on Sep 29, 2009 10:38
    Good Article. Keep going with some real time scenarios.
  • iiordanov

    RE: Tip: How to declare an attached property in Silverlight?


    posted by iiordanov on Oct 02, 2009 10:18

    Hi Maddy,

    Yes, we have to show some more real time scenarios.

Add Comment

Login to comment:
  *      *       
Login with Facebook