
XAML Property-element Syntax
Within the scope of an opening element, you will be able to set values to the properties and events of the type. For
example, the following <Button> sets the Content, Height and Width properties and handles the Click event. The
name assigned to the Click event will map to a method in your code file.
<Button Name = "myButton" Height = "100" Width = "100"
Click = "myButton_Click" Content = "OK" />
Here, Content, Height, Width, and Click were assigned values that could be captured as a simple string.However, many
properties of classes do not operate on data that represents simple string values. For example, some properties require
full-blown objects (complex brushes, pens, and so on).
XAML property-element syntax allows you to assign complex object values to a property. This syntax defines a
subscope scope representing a property of the defining type. Within this scope, you can describe the object to be used
for the property assignment. The format follows the following template:
<DefiningType>
<DefiningType.PropertyOnDefiningType>
<!-- data used to set property -->
</DefiningType.PropertyOnDefiningType>
</DefiningType>
Consider the following syntax, which sets the Background property of a Button to a LinearGradientBrush type using
property-element syntax. The associated image shows you how this button would look when rendered by a XAML
parser.
<Button x:Name = "myButton" Height = "100"
Width = "100" Content = "Click Me!">
<Button.Background>
<LinearGradientBrush StartPoint = "0,0" EndPoint = "1,1">
<GradientStop Color = "Blue" Offset = "0" />
<GradientStop Color = "Yellow" Offset = "0.25" />
<GradientStop Color = "Green" Offset = "0.75" />
<GradientStop Color = "Pink" Offset = "0.50" />
</LinearGradientBrush>
</Button.Background>
</Button>
XAML Attached-Property Syntax
XAML also makes use of a concept termed ‘attached property’ syntax. One use of attached properties is to make it
possible for a sub-element to assign a property value on a parent element. In this case, the template to use looks like the
following:
<ParentType>
<ChildType ParentType.PropertyName = "Value">
</ChildType>
</ParentType>
The most common use of attached-property syntax is to position UI elements within one of the panel types (such as
the Grid). You will dive into these panels in some detail later. However, here is an example of attached-property syntax.
<UserControl
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
Height = "300" Width = "300">
<Grid Background="White" Width="450" Height="200" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="Testing, 1, 2, 3!"/>
<TextBlock Grid.Column="0" Grid.Row="1" Text = "Rectangle:"/>
<Rectangle Grid.Column="1" Grid.Row="1" Fill="Purple" Width="250" Height="50"/>
<TextBlock Grid.Row="3" Grid.Column="0" Text = "Another test"/>
<TextBlock Grid.Row="3" Grid.Column="1" Text = "Final test"/>
</Grid>
</UserControl>
The rendered output would look like so:
Understand that attached properties cannot be used as a general-purpose syntax that can be used to set any property on
a parent element. The IntelliSense of Visual Studio will show you valid attached properties for a given element.
In reality, attached properties are a specialized form of a concept termed dependency property. Thus, attached-property
syntax can only work if the property supports the correct dependency property infrastructure. You will learn more
about this infrastructure later in the class.
XAML Property-element Syntax
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services