WPFwiki, wpf, wiki, .net 3.0, windows presentation foundation, FAQ, free resources, solution, development, microsoft Home of the world's largest WPF FAQ
Edit

Category

Fundamentals

Edit

Question

How do I delcare an Attached DependencyProperty?

Edit

Answer

First of all, as with a regular DependencyProperty, an Attached DependencyProperty should be stored in a public static readonly field. This is to ensure its visibility to anyone wishing to use it.

There is an informal naming convention for the field. Namely, it should be the DependencyProperty's name suffixed with the "Property" token. This convention is the same for all DependencyProperty types (normal, attached, readonly, and attached readonly).

You then need to call the DependencyProperty.RegisterAttached() function.

In this function, you specify the property's name, type, owner type, default value (if desired) and callbacks (if desired).

Example (with a default value of -125 and no callback):

 
public CustomClass1: DependencyObject
{
 
    public static readonly DependencyProperty MyCustomProperty = DependencyProperty.RegisterAttached( "MyCustom", typeof(int), typeof(CustomClass1), new PropertyMetadata(-125) );
 
}

All content is Copyright ©2007 Xceed Software Inc. unless otherwise indicated. See the Terms of Service. Contributors must read and agree to the Contribution Policy. WPFwiki is brought to you by Xceed, makers of the powerful yet free Xceed DataGrid for WPF.