(X) Hide this Join a live webcast on October 14th, 10:00 am PDT: 'MEF: Overview of the Managed Extensibility Framework in Silverlight 4' by Gill Cleeren
Learn More | Sign Up | More Webinars by SilverlightShow

Recommended

Skip Navigation LinksHome / Tips / View Tip

Tip: How to declare an attached property in Silverlight?

+ Add to SilverlightShow Favorites
2 comments   /   posted by Ilia Iordanov on Sep 10, 2008
(21 votes)
Categories: General , Controls and UI

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!

Share


Comments

Comments RSS RSS
  • 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.
  • 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

 
 

   
  
  
   
Please add 1 and 3 and type the answer here: