XAML Flashcards
Chapter 4
XAML Tree of Objects
One of the most important concepts of WPF is that the user interface is a tree of WPF class objects. That tree has a single node at the top, and each node in the tree can have zero or more child nodes.
Object Element Syntax
Objects in the UI are represented by XAML elements:
• The start tag opens with an open angle bracket (). Immediately to the right of the open angle bracket is the type name of
the element. Using object element syntax, the type name must match the name of
the WPF class.
• The end tag starts with the two-character string, an open angle bracket followed
by a slash, which is followed by the type name of the element. The end tag signals
the end of the element.
• The content section is the area between the start tag and the end tag; it can
contain text, other elements, or white space. Any nested elements might, in turn,
contain other nested elements.
• Whatever is in the content section of the element is assigned to the default content
property of the class.
XAML Trees
• A XAML tree is made up of elements.
• Elements must be properly nested. The nesting structure of the elements
determines the structure of the XAML tree and hence the structure of the UI.
• The top, or outer, element is the root element and contains all the other elements.
Attribute Syntax
The important things to know about attributes are the following:
• Attributes must be placed inside the start tag, following the element name. They
cannot be in the content area or the end tag.
• The syntax of an attribute consists of an identifier that is a property name,
followed by an equals sign, followed by a string demarcated by a set of double
quotation marks or single quotation marks.
• An element can have any number of attributes, which must be separated by white
space—not commas.
empty element tag syntax
− This form consists of a single tag, instead of a start and end tag.
− The tag has the syntactic form of a start tag—including allowing attributes—
but uses the /> string as its end delimiter instead of >.
Object element syntax details
• The element name can be either a .NET class or a .NET struct. Throughout the
text, however, to avoid awkwardness, I’ll just refer to the element name as a class
name.
• The element name must exactly match the name of the corresponding .NET class.
Remember that XAML is case sensitive.
• When creating an object from the XAML element, the XAML parser always uses
the class’s parameterless constructor. You cannot specify a different constructor.
• The element is translated into a class object in the following steps:
a. Create the object, using the parameterless constructor.
b. Set the default content property of the class object with the content part of
the XAML element. (I’ll describe more about the default content property in
the next section.)
c. Set the other class object properties to the values assigned to in XAML
attributes.
Syntax examples
“StackPanel”
“Button”Click Me”/Button” Object Element Syntax
“Button Content=”Click Me””“/Button” Attr bute Syntax
“Button Content=”Click Me”/” Attr bute Syntax, Empty Element
“Button Button.Content=”Click Me”/”
“/StackPanel”
Type Converters for Attributes
the Background property of a Button must be set with an object derived from the
Brush class, but the attribute sets it with the simple string “Blue”. As a matter of fact, attribute syntax
requires that attributes be set with strings
The short answer is that the XAML parser converts it for you. The parser uses the class and property name to check the type of the property, and uses a TypeConverter for that type to convert the string to an object of the type required by the property.
Property Element Syntax
• The property is listed, not as an attribute inside the start tag, but using element
syntax nested in the content part of the object element.
• The element name uses a two-part name in dot-syntax notation. The name
consists of the class name and the property name, separated by a period (dot), as
shown in the following syntax code:
{ElementTypeName.PropertyName}
Value
{/ElementTypeName.PropertyName}
Attached Property Syntax
Attached properties are a special type of property that is defined in one class but used in another
{Button Grid.Row=”2”}Click{/Button}
Code-Behind and Object Names
Many classes have a Name property, which you can set using an attribute, if you need to
manipulate the object, as shown in the following line of code. Using this form, you can now refer to the
button in the code-behind with the name myButton.
{Button Name=”myButton”}Click Me{/Button}
Some classes, however, don’t have a Name property. In this case, you can use a XAML construct
for assigning a name, as shown in the following line of code. Using this format you can also refer to the
object as myButton in the code-behind.
{Button x:Name=”myButton”}Click Me{/Button}
Namespaces
• A XAML namespace is a case-sensitive string that represents a collection of .NET types. • Namespaces are assigned using the xmlns attribute in the start tag of the root element of the XAML document. • If you want to use a class as an element in your XAML markup, you must give the XAML parser the namespace that contains the class. • Two standard namespaces represent the WPF classes and the XAML classes. Their names look like web addresses. These are shown in the following lines of markup: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Default namespace
One, and only one, namespace can be the default
namespace.
• You declare the default namespace using the keyword xmlns, followed by an
equals sign, followed by a string that is the name of the default namespace.
• When you use types from the default namespace, you can use the type names
directly, without further qualification. For example, the examples you’ve seen so
far have used classes like Button and TextBlock, which have been in the default
namespace.
• You can set the default namespace to be any namespace, but it makes the most
sense to set it to the namespace that contains the WPF types, which is the
namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation.
Non-default (other) namespaces
• You declare a nondefault namespace using the keyword xmlns, followed by a
colon, followed by a string, followed by an equals sign and the name of the
namespace. The string following the colon is called the prefix. Generally, you want
the prefix to be a short string so it’s easier to use.
• When you use a type from a nondefault namespace, you must use the prefix,
followed by a colon, followed by the type name.
Using Classes from Other Namespaces
xmlns: local=”clr-namespace:OtherPart”
xmlns: lib=”clr-namespace:MyLib;assembly=MyAssembly”
{local:MyButton}Hi There{/local:MyButton}