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

Tip: How to declare a dependency property in Silverlight?

(10 votes)
Ilia Iordanov
>
Ilia Iordanov
Joined Oct 25, 2007
Articles:   12
Comments:   89
More Articles
5 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 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!


Subscribe

Comments

  • -_-

    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 Narasimha on Feb 25, 2011 08:27

    Hi All,

     Ofter declare the DP. how can access the same control in my a XAML design please let me know .

     

  • TemalaRidha

    Re: Tip: How to declare a dependency property in Silverlight?


    posted by TemalaRidha on Oct 10, 2011 11:39

    To access this dependecy preperty you have to first acces to the namespace throught the xaml code:

      xmlns:Controls="clr-namespace:YourProjcet.NameSpaceWhereYouDeclaredYourControl"    

    and then simply call your user control:

    <Controls:YourControl   YourDependencyProperty={ you can even bind to a property} />

Add Comment

Login to comment:
  *      *       
Login with Facebook