Attached Properties in Silverlight


 

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

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

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s