Section 3.4. Attached Properties


3.4. Attached Properties

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> 




XAML in a Nutshell
XAML in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596526733
EAN: 2147483647
Year: 2007
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net