Skip Navigation LinksHome / Tips / View Tip

Tip: How to declare a dependency property in Silverlight?

+ Add to SilverlightShow Favorites
4 comments   /   posted by Ilia Iordanov on Sep 10, 2008
(3 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 dependency properties. This is needed because either to set or to get value for a specific dependency property, you need to use the methods SetValue and GetValue which are defined in the DependencyObject class.
When you declare a dependency property, many advantages 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 dependency property in Silverlight, you have to follow few simple steps as explained in the sample code below.

C#

public partial class MySilverlightControl : UserControl
{
    //1. Declare the dependency property as static, readonly field in your class.
    public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
        "MyProperty",                               //Property name
        typeof( string ),                           //Property type
        typeof( MySilverlightControl ),             //Type of the dependency property provider
        new PropertyMetadata( MyPropertyChanged ) );//Callback invoked on property value has changes
 
    public MySilverlightControl()
    {
        InitializeComponent();
    }
 
    //2. Create the property and use the SetValue/GetValue methods of the DependencyObject class
    public string MyProperty
    {
        set
        {
            this.SetValue( MyPropertyProperty, value );
        }
        get
        {
            return ( string )this.GetValue( MyPropertyProperty );
        }
    }
 
    private static void MyPropertyChanged( object sender, DependencyPropertyChangedEventArgs args )
    {
        //Do some processing here...
    }
}

Read more about DependencyObject and DependencyProperty classes on MSDN.

That's it!

Share


Comments

Comments RSS RSS
  • RE: Tip: How to declare a dependency property in Silverlight?  

    posted by Hector on May 28, 2009 11:54
    so how do you make an property that expects an IEnumerable such as ItemsSource from a ComboBox, and what if you have two or more controls that you want to add a dependy property to make data-bindable?  For example, if I had two ComboBoxes in your example above, the MySilverLightControl, how do I specify which ComboBox a property belongs to?
  • RE: Tip: How to declare a dependency property in Silverlight?  

    posted by Nishanth on Aug 12, 2009 13:46

    Below is an example.

    The default value and the propertychanged handler are optional. Depends on whether you want something besides null as the default value if the setter is never called, and whether you want to execute some code when the value changes. Note that the naming of properties are important. Search/Replace "MyProperty" with whatever you want to call it, but not that the stafic DP must be names [name]Property.

    public MyObjectType MyProperty
    {
    get { return (MyObjectType)GetValue(MyPropertyProperty); }
    set { SetValue(MyPropertyProperty, value); } //Never put code here! Put it in onMyPropertyChanged
    }

    public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.Register("MyProperty", typeof(MyObjectType), typeof(MyControl), new PropertyMetadata([DefaultValue], onMyPropertyChanged));

    private static void onMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    MyControl obj = d as MyControl;
    //handle changed values
    }

    Next step is to create a Control with a Template. You can then bind this value into your control using:

    <TextBox Text="{TemplateBinding MyProperty}" /> 

    You might want to see this post on how to create your own custom controls: http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx

  • RE: Tip: How to declare a dependency property in Silverlight?  

    posted by Andy Bruce on Jul 17, 2010 23:42
    Hey, thanks for this resource!
  • RE: Tip: How to declare a dependency property in Silverlight?  

    posted by 56565 on Jul 27, 2010 15:08
    565656

Add Comment

 
 

   
  
  
   
Please add 2 and 4 and type the answer here:

Join the free SilverlightShow webcast 'Running Silverlight Outside the Browser and with Elevated Trust'. Sept 7th, 8 am - 9 am PDT.
In this live session Chris Anderson will cover configuring and debugging OOB mode, toast notifications, elevated trust, direct file access and much more.
Learn more | Register | See more webinars (hide this)