Properties and More in XAML (Page 1 of 4 )
In this third part of a three-part series covering the basics of XAML, you will learn about attached properties, binding properties, and more. It is excerpted from the book
XAML in a Nutshell, written by Lori A. MacVittie (O'Reilly, 2006; ISBN: 596526733). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Abbreviated Syntax
Abbreviated syntax must sound like jabberwocky at this point, but it’s really a pretty neat concept. It uses a predefined format, such as CSV (comma-separated values), to essentially declare the arguments that will be passed to the appropriate class constructor beneath the covers. You can think of the String definition as the list of arguments you’d normally pass to a constructor, except that sometimes you don’t need a comma to separate the arguments.
Example 3-11 first creates an EllipseGeometry in C# and then assigns a Point to be its Center property by instantiating a new Point and passing the appropriate values to its constructor. The XAML code in the same example creates an EllipseGeometry and then assigns a Point to be its Center attribute using abbrevi ated syntax. The 0.5, 0.5 is parsed by the WPF engine, and the values are passed to a Point constructor as its arguments.
Example 3-11. Abbreviated syntax and arguments in C#
C #
EllipseGeometry ellipse;
ellipse.Center=new Point(0.5, 0.5);
XAML
<EllipseGeometry
Center="0.5,0.5" />
A very common example of using abbreviated syntax to declare attribute values is the assignment of predefined color names to an attribute declared as type Brush , such as the Background attribute. Rather than forcing you to go through all the typing required to explicitly declare a Brush and set its color, XAML allows you to just declare the attribute as Red or Green instead.
Figure 3-3 shows the result of declaring a Red SolidColorBrush as the background Brush for a Button using both abbreviated markup and by explicitly declaring the complex attribute. Example 3-12 shows the code used to declare both elements.

Figure 3-3. Result of explicit declaration and abbreviated markup declaration of a Brush attribute on a Button
Example 3-12. Explicit declaration of a Brush versus abbreviated markup
<StackPanel
xmlns=http://schemas.microsoft.com/ winfx/avalon/2005
Margin="10 10 10 10">
<Button
Width="350"
Height="30"
Content="Button with explicitly declared Background Brush">
<Button.Background>
<SolidColorBrush Color="Red" />
</Button.Background>
</Button>
<Button
Width="350"
Height="30"
Background="Red"
Content="Button with a Background Brush declared using abbreviated
markup"/>
</StackPanel>
As you can see in Figure 3-3, both buttons are painted with the same back ground, regardless of the method used to declare the Brush . Abbreviated syntax is typically used because it requires less typing. There are no advantages to using explicit syntax in most cases where abbreviated syntax is available, and it’s less typing for you.
A more complex example is the common use of abbreviated markup syntax to declare elements of the type Point . Point is a common, complex attribute that is used in the declaration of almost every geometric XAML element. You can use the abbreviated markup syntax for a Point element wherever an element of type Point is declared. You’ll notice in Example 3-13 that EllipseGeometry has several attributes. While RadiusX and RadiusY are Double values, the Center attribute for an EllipseGeometry is actually a complex attribute of type Point . In its abbreviated syntax, Point accepts two comma-separated values representing the X and Y positions, respectively. Example 3-13 shows different ways of using Point (in this case, it is used through the Center attribute).
Example 3-13. Example of abbreviated markup versus explicit syntax
<GeometryGroup>
<EllipseGeometr y
RadiusX="0.45"
RadiusY="0.2"
Center="0.5,0.5" />
<EllipseGeometry
RadiusX="0.2"
RadiusY="0.45">
<EllipseGeometry.Center>
<Point
X="0.5"
Y="0.5" />
</EllipseGeometry.Center>
</EllipseGeometry>
</GeometryGroup>
Elements that can be declared using abbreviated markup syntax are specifically noted in Part III.
Next: Attached 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.
|
|