Udostępnij za pośrednictwem


How to: Use Attached Properties

Attached properties are a way to extend types with properties that are provided by other types. You can use attached properties to specify the relationship of a child element to its parent element. For example, to place a button object in a particular cell of a grid object, you can use the Row and Column attached properties of the Grid object. The code looks like the following:

<Button   Name="btnOk" Grid.Column="0" Grid.Row="0" >OK</Button>
System.Windows.Controls.Grid.SetRow(btnOk, 0)
System.Windows.Controls.Grid.SetColumn(btnOk, 0)
System.Windows.Controls.Grid.SetRow(btnOk, 0);
System.Windows.Controls.Grid.SetColumn(btnOk, 0);

For more information, see Attached Properties Overview.

When you use the WPF Designer for Visual Studio, you can access attached properties through the Properties window. You can identify attached properties because they are preceded with the type that provides the property. As you change the layout of controls in the designer, the attached properties are updated automatically. Conversely, when you change the attached properties in the Properties window, the designer is updated automatically.

To set the value of an attached property

  1. In the WPF Designer, select the child object for which you want to set an attached property. For example, select a button that is on a grid.

  2. In the Properties window, locate the attached property and set the value. For example, locate the Grid.Column property, and set the value to the index of the column of the grid in which you want the button to appear.

See Also

Tasks

How to: Register an Attached Property

Concepts

Dependency Properties Overview

Other Resources

XAML and Code in the WPF Designer