Recommended

Skip Navigation LinksHome / Search

Search

 
Results Per Page

Found 1 result for AttachedProperty.
Date between: <not defined> and <not defined>
Search in: News , Articles , Tips , Shows , Showcase , Books

Order by Publish Date   Ascending Title   Rating  

  • 2 comments  /  posted by  Ilia Iordanov  on  Sep 10, 2008 (more than a year ago)

    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!




Help us make SilverlightShow even better and win a free t-shirt. Whether you'd like to suggest a change in the structure, content organization, section layout or any other aspect of SilverlightShow appearance - we'd love to hear from you! Need a material (article, tutorial, or other) on a specific topic? Let us know and SilverlightShow content authors will work to have that prepared for you. (hide this)