Elements and Attributes in XAML - Attributes
(Page 3 of 4 )
Attributes are the XML representation of the properties of an element’s corresponding CLR class. The Width attribute of the XAML Button element corresponds directly to the Width property of the System.Windows.Button class. To show the correlation between XAML and CLR classes, Examples 3-7 and 3-8 declare a Button instance and its attributes in both XAML and C#.
Example 3-7. Button declared in XAML
<Button
Width="100"
Name="myButton"
Height="20"
Content="This is my button" />
Example 3-8. Button declared in C#
Button myButton ;
myButton.Width=100;
myButton.Height=20;
myButton.Content = "This is my button";
As with the XAML tags for elements, attributes are spelled exactly the same as their corresponding CLR class properties. ( Width = Width , Content = Content ... You get the picture.)
There are two types of XAML attributes. The first, dependency properties, are public static read-only fields on CLR classes that are derived from DependencyProperty and have declared CLR accessor methods. In other words, the value of dependency properties can be dependent on (hence the name) other variables in CLR classes and, therefore, can only be accessed with a public get or set accessor method to be evaluated properly.
Dependency properties are like stock certificates. The stock certificate represents a value (money), but the actual amount of money it is worth (its value) is
deter mined by external calculations and can change at nearly any time. To determine the value of your stock certificate, you must consult the stock exchange and do some multiplication. Dependency properties can also be based on external resources and often rely on calculations to determine their value.
Dependency property values are determined from a number of different places. The WPF property system searches for the value from the following places in this order:
Storyboards or event triggers that start an animation; property values set by an animation override even local values
Local value (i.e., <Object Property="value"> )
- Property triggers
- TemplatedParent ’s template (i.e., that template includes <Setter> )
- Style property
- ThemeStyle
- Inheritance (from your parent element, not your superclass)
- DefaultValue specified when you registered the property (or override metadata)
These attributes provide support for value expressions, property invalidation, default values, inheritance, data binding, animation, and styling. The property system is complex, so WPF provides simple get and set accessor methods to manipulate these attributes.
The second type of attribute supported in XAML is the common language runtime property. Common language runtime properties are standard read/write CLR class properties that can be accessed directly and do not require get or set accessor methods, although they generally have them.
Both dependency properties and common runtime properties are accessed in XAML using the same techniques. The difference between them is important only when you are using more advanced techniques, such as defining styles or triggers that act upon a specific attribute. Some attributes of elements must reference a dependency property, so you need to know which attributes are dependency properties and which are not.
Next: Assigning XAML Attributes >>
More XML Articles
More By O'Reilly Media
|
This article is excerpted from chapter 3 of The Basics of XAML, written by Lori A. MacVittie (O'Reilly, 2006; ISBN: 0596526733). Check it out today at your favorite bookstore. Buy this book now.
|
|