Properties and More in XAML - Attached Properties
(Page 2 of 4 )
A few XAML elements have attributes that are declared in other elements rather than in the element itself. These attributes are called attached properties. Attached properties are generally used to position elements within a parent element. Two elements with attached properties are Grid and DockPanel . Grid uses attached properties to describe the row and column in which an element should be contained. DockPanel uses attached properties to describe the location within the panel where an element should be placed.
Attached properties can be set on any element that derives from DependencyObject . UIElement derives from DependencyObject , so the requirement is met by most XAML elements.
Attached properties are declared in an element by using a reference to the element and the attribute being declared in the following manner: AttachPropertyProvider.PropertyName . For example, Grid has two attached properties: Row and Column . An element contained within a specific row/column combination in a grid would specify the row as an attribute with the name Grid.Row and the column similarly as Grid.Column . Example 3-14 describes the use of these attached properties.
Example 3-14. Using the attached properties of Grid
<Grid
ShowGridLines="true">
<ColumnDefinition
Width="50"/>
<ColumnDefinition
Width="50"/>
<RowDefinition
Height="100" />
<RowDefinition
Height="25" />
<RowDefinition
Height="25" />
<TextBlock
Grid.Column="0"
Grid.Row="0">Col 0, Row 0
</TextBlock>
<TextBlock
Grid.Column="1"
Grid.Row="0">Col 1, Row 0
</TextBlock>
<TextBlock
Grid.Column="0"
Grid.Row="1">Col 0, Row 1
</TextBlock>
<TextBlock
Grid.Column="1"
Grid.Row="1">Col 1, Row 1
</TextBlock>
</Grid>
Next: Binding Properties >>
More XML Articles
More By O'Reilly Media
|
This article is excerpted from the book XAML in a Nutshell, written by Lori A. MacVittie (O'Reilly, 2006; ISBN: 596526733). Check it out today at your favorite bookstore. Buy this book now.
|
|