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.............
}
}
}