[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]
XAML Syntax Introduction
This article describes how to use several different methods to create objects and set their properties in XAML.
The topic includes the following parts:
What is XAML?
Declare object
Set properties
Other related topics
What is XAML
XAML is a declarative language. You can use XAML tags to create visual UI originals. After that, you can use JavasScript in a separate file to manipulate the objects you declared in XAML and respond to some events. As an XML-based declarative language, the process from prototype to product is very intuitive when creating interfaces, especially for those with background knowledge and technology in web design.
XAML files are usually XML files with .xaml as the suffix. Below is a typical Silverlight XAML file example. .XAML
<Canvas
xmlns="/client/2007"
xmlns:x="/winfx/2006/xaml">
<Rectangle
Width="100"
Height="100"
Fill="Blue" />
</Canvas>
Declare object
In XAML, there are several ways to declare objects and set their properties:
Object element syncax: Use open and enclosed tags to declare objects, just like XML. You can use this method to declare root elements and set their attribute values.
Attribute syntax: Use inline to declare objects. You can use this method to set the value of a property. .
Object element syntax
A typical method of declaring an object using Object element syncax. .First you want to create two XML element tags:
<objectName>
</objectName>
... objectName is the name of the object you want to instantiate. The following example uses Object element syncax to declare a Canvas. XAML
<Canvas>
</Canvas>
Some objects, such as Canvas, can contain other objects. .XAML
<Canvas>
<Rectangle>
</Rectangle>
</Canvas>
For convenience, if an object does not contain other objects, you can use only one tag to describe it XAML
<Canvas>
<Rectangle />
</Canvas>
Use Attribute syntax to declare objects
See the next section, Set properties, and get more information about attribute syntax.
Set properties
Using object element syncax, you can set its properties when declaring an object. In XAML, there are several ways to set properties: use attribute syncax, or use property element syncax.
Set attributes via Attribute syntax
<objectNameproperty="propertyValue">
</objectName>
...property is the property name, and you will assign the value of propertyValue to it. The following example shows how to use attribute syntax to set a Rectangle's Width, Height, and Fill .XAML
<Canvas>
<Rectangle
Width="100"Height="100"Fill="Blue" />
</Canvas>
Set properties using PropertyElement Syntax
Some properties can be set through property element syncax. You describe the properties you want by creating XML elements, for example:
<objectName>
<>
<propertyValue ... />
</>
</objectName>
...property is the property name, you will assign the value of propertyValue to it. The following example shows how to use property element syntax to set a Rectangle Fill, use a
<Canvas>
<Rectangle
Width="100"
Height="100">
<>
<SolidColorBrush />
</>
</Rectangle>
</Canvas>
Set properties using Content Element Syntax
Sometimes, when an attribute supports element syntax, you can ignore the attribute name and directly embed the attribute value in the object label. This is content element syncax. The following example shows how to set the Text attribute value of TextBlock without specifying the Text attribute. XAML
<TextBlock>
Hello!
</TextBlock>
Set properties using Implicit Collection Syntax
Sometimes, an attribute is manifested as a collection. You can ignore the collection name and set the attribute value directly. This is implicit collection syncax. The following example shows how to ignore GradientStopCollection for LinearGradientBrush and directly specify the GradientStop object. GradientStopCollection is included in the first LinearGradientBrush, but is ignored in the second. XAML
<Rectangle Width="100" Height="100"
="0" ="30">
<>
<LinearGradientBrush>
<>
<!-- Here the GradientStopCollection tag is specified. -->
<GradientStopCollection>
<GradientStop Offset="0.0" Color="Red" />
<GradientStop Offset="1.0" Color="Blue" />
</GradientStopCollection>
</>
</LinearGradientBrush>
</>
</Rectangle>
<Rectangle Width="100" Height="100"
="100" ="30">
<>
<LinearGradientBrush>
<>
<!-- Notice that the GradientStopCollection tag
is omitted. -->
<GradientStop Offset="0.0" Color="Red" />
<GradientStop Offset="1.0" Color="Blue" />
</>
</LinearGradientBrush>
</>
</Rectangle>
Sometimes you can even ignore the collection element tag and attribute element tag at the same time::XAML
<Rectangle Width="100" Height="100"
="200" ="30">
<>
<LinearGradientBrush>
<GradientStop Offset="0.0" Color="Red" />
<GradientStop Offset="1.0" Color="Blue" />
</LinearGradientBrush>
</>
</Rectangle>
When to set properties using Attribute or Property Element Syntax
All properties support attribute or property element syncax, and some properties support other methods. The methods supported by setting properties depend on the object type recognized by the property value. .
If the attribute value is a simple type, such as Double, Integer, String, this attribute only supports attribute syntax. The following example shows how to use attribute syntax to set the attribute of Rectangle to support Attribute syntax because its attribute value is Double type. XAML
<Rectangle Width="100" />
Whether you can use attribute syntax depends on whether the object you use to set attributes supports attribute syntax. The following example shows using attribute syntax to set a Fill attribute of a Rectangle. When you use SolidColorBrush to set the Fill attribute, it supports attribute syntax, because SolidColorBrush supports attribute syntax. XAML
<Rectangle Fill="Blue" />
Whether you can use element syntax to set properties depends on whether the object you are using supports it. If the object supports object element syntax, the property only supports property element syntax . The following example shows using property element syncax to set a Rectangle Fill. When you set Fill with SolidColrBrush, it supports attribute syncax because SolidColorBrush supports attribute syncax. .XAML
<Rectangle>
<>
<SolidColorBrush />
</>
</Rectangle>
See Also
Silverlight Object Models
XAML Syntax Introduction
This article describes how to use several different methods to create objects and set their properties in XAML.
The topic includes the following parts:
What is XAML?
Declare object
Set properties
Other related topics
What is XAML
XAML is a declarative language. You can use XAML tags to create visual UI originals. After that, you can use JavasScript in a separate file to manipulate the objects you declared in XAML and respond to some events. As an XML-based declarative language, the process from prototype to product is very intuitive when creating interfaces, especially for those with background knowledge and technology in web design.
XAML files are usually XML files with .xaml as the suffix. Below is a typical Silverlight XAML file example. .XAML
<Canvas
xmlns="/client/2007"
xmlns:x="/winfx/2006/xaml">
<Rectangle
Width="100"
Height="100"
Fill="Blue" />
</Canvas>
Declare object
In XAML, there are several ways to declare objects and set their properties:
Object element syncax: Use open and enclosed tags to declare objects, just like XML. You can use this method to declare root elements and set their attribute values.
Attribute syntax: Use inline to declare objects. You can use this method to set the value of a property. .
Object element syntax
A typical method of declaring an object using Object element syncax. .First you want to create two XML element tags:
<objectName>
</objectName>
... objectName is the name of the object you want to instantiate. The following example uses Object element syncax to declare a Canvas. XAML
<Canvas>
</Canvas>
Some objects, such as Canvas, can contain other objects. .XAML
<Canvas>
<Rectangle>
</Rectangle>
</Canvas>
For convenience, if an object does not contain other objects, you can use only one tag to describe it XAML
<Canvas>
<Rectangle />
</Canvas>
Use Attribute syntax to declare objects
See the next section, Set properties, and get more information about attribute syntax.
Set properties
Using object element syncax, you can set its properties when declaring an object. In XAML, there are several ways to set properties: use attribute syncax, or use property element syncax.
Set attributes via Attribute syntax
<objectNameproperty="propertyValue">
</objectName>
...property is the property name, and you will assign the value of propertyValue to it. The following example shows how to use attribute syntax to set a Rectangle's Width, Height, and Fill .XAML
<Canvas>
<Rectangle
Width="100"Height="100"Fill="Blue" />
</Canvas>
Set properties using PropertyElement Syntax
Some properties can be set through property element syncax. You describe the properties you want by creating XML elements, for example:
<objectName>
<>
<propertyValue ... />
</>
</objectName>
...property is the property name, you will assign the value of propertyValue to it. The following example shows how to use property element syntax to set a Rectangle Fill, use a
<Canvas>
<Rectangle
Width="100"
Height="100">
<>
<SolidColorBrush />
</>
</Rectangle>
</Canvas>
Set properties using Content Element Syntax
Sometimes, when an attribute supports element syntax, you can ignore the attribute name and directly embed the attribute value in the object label. This is content element syncax. The following example shows how to set the Text attribute value of TextBlock without specifying the Text attribute. XAML
<TextBlock>
Hello!
</TextBlock>
Set properties using Implicit Collection Syntax
Sometimes, an attribute is manifested as a collection. You can ignore the collection name and set the attribute value directly. This is implicit collection syncax. The following example shows how to ignore GradientStopCollection for LinearGradientBrush and directly specify the GradientStop object. GradientStopCollection is included in the first LinearGradientBrush, but is ignored in the second. XAML
<Rectangle Width="100" Height="100"
="0" ="30">
<>
<LinearGradientBrush>
<>
<!-- Here the GradientStopCollection tag is specified. -->
<GradientStopCollection>
<GradientStop Offset="0.0" Color="Red" />
<GradientStop Offset="1.0" Color="Blue" />
</GradientStopCollection>
</>
</LinearGradientBrush>
</>
</Rectangle>
<Rectangle Width="100" Height="100"
="100" ="30">
<>
<LinearGradientBrush>
<>
<!-- Notice that the GradientStopCollection tag
is omitted. -->
<GradientStop Offset="0.0" Color="Red" />
<GradientStop Offset="1.0" Color="Blue" />
</>
</LinearGradientBrush>
</>
</Rectangle>
Sometimes you can even ignore the collection element tag and attribute element tag at the same time::XAML
<Rectangle Width="100" Height="100"
="200" ="30">
<>
<LinearGradientBrush>
<GradientStop Offset="0.0" Color="Red" />
<GradientStop Offset="1.0" Color="Blue" />
</LinearGradientBrush>
</>
</Rectangle>
When to set properties using Attribute or Property Element Syntax
All properties support attribute or property element syncax, and some properties support other methods. The methods supported by setting properties depend on the object type recognized by the property value. .
If the attribute value is a simple type, such as Double, Integer, String, this attribute only supports attribute syntax. The following example shows how to use attribute syntax to set the attribute of Rectangle to support Attribute syntax because its attribute value is Double type. XAML
<Rectangle Width="100" />
Whether you can use attribute syntax depends on whether the object you use to set attributes supports attribute syntax. The following example shows using attribute syntax to set a Fill attribute of a Rectangle. When you use SolidColorBrush to set the Fill attribute, it supports attribute syntax, because SolidColorBrush supports attribute syntax. XAML
<Rectangle Fill="Blue" />
Whether you can use element syntax to set properties depends on whether the object you are using supports it. If the object supports object element syntax, the property only supports property element syntax . The following example shows using property element syncax to set a Rectangle Fill. When you set Fill with SolidColrBrush, it supports attribute syncax because SolidColorBrush supports attribute syncax. .XAML
<Rectangle>
<>
<SolidColorBrush />
</>
</Rectangle>
See Also
Silverlight Object Models