Attached Properties in Silverlight


1. Bellow you can see how to assign value to an attached property from within C# and XAML:

TabPanel.SetTabStop( uiElement, true );
uiElement.SetValue( TabPanel.TabStopProperty, true );
    <Button TabPanel.TabStop="True"/>

As you can see here the attached property is TabPanel.TabStop and the TapPanel is the property provider that has registered it internally. This registration is explained later in our example.

2. Here is how our TabPanel class finally should look like:

public class TabPanel : StackPanel
    publicTabPanel() {}
    public static readonly DependencyProperty TabStopProperty = DependencyProperty.RegisterAttached(
             "TabStop",                  //Name of the property
             typeof( bool ),             //Type of the property
             typeof( TabPanel ),   //Type of the provider of the registered attached property
             null );                           //Callback invoked in case the property value has changed

    public static void SetTabStop( DependencyObject obj, booltabStop )     {
             obj.SetValue( TabStopProperty, tabStop );      }

    public static bool GetTabStop( DependencyObject obj )     {
             return ( bool )obj.GetValue( TabStopProperty );  }
    //Just a sample method ilustrating the idea how to obtain the value
    //of the TabStop property for a specific element.
    private void ProcessTabKey() {
             foreach ( UIElement element in this.Children ) {
                                   bool tabStop = TabPanel.GetTabStop( element );
                                   //Perform some processing according to the tab stop… }

Via Attached Properties in Silverlight


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s