Documentos de Académico
Documentos de Profesional
Documentos de Cultura
XA M L Syntax In Detail
8/22/12
XA M L Syntax In Detail
Properties and events as they appear as XAML members of a WPF type are often inherited from base types. For example, consider this example: < u t n B c g o n = B u " . . > The Bto akrud"le ./. Background property is not an immediately declared property on the Button class, if you were to look at the class definition, reflection results, or the documentation. Instead, Background is inherited from the base Control class. The class inheritance behavior of WPF XAML elements is a significant departure from a schemaenforced interpretation of XML markup. Class inheritance can become complex, particularly when intermediate base classes are abstract, or when interfaces are involved. This is one reason that the set of XAML elements and their permissible attributes is difficult to represent accurately and completely using the schema types that are typically used for XML programming, such as DTD or XSD format. Another reason is that extensibility and typemapping features of the XAML language itself preclude completeness of any fixed representation of the permissible types and members.
Content Models
A class might support a usage as a XAML object element in terms of the syntax, but that
msdn.microsoft.com/en-us/library/ms788723 2/13
8/22/12
XA M L Syntax In Detail
element will only function properly in an application or page when it is placed in an expected position of an overall content model or element tree. For example, a MenuItem should typically only be placed as a child of a MenuBase derived class such as Menu. Content models for specific elements are documented as part of the remarks on the class pages for controls and other WPF classes that can be used as XAML elements.
Note
You can use alternating quotes to place a literal quotation mark within an attribute. For instance you can use single quotes as a means to declare a string that contains a double quote character within it. Whether you use single or double quotes, you should use a matching pair for opening and closing the attribute value string. There are also escape sequences or other techniques available for working around character restrictions imposed by any particular XAML syntax. See XML Character Entities and XAML. In order to be set through attribute syntax, a property must be public and must be writeable. The value of the property in the backing type system must be a value type, or must be a reference type that can be instantiated or referenced by a XAML processor when accessing the relevant backing type. For WPF XAML events, the event that is referenced as the attribute name must be public and have a public delegate. The property or event must be a member of the class or structure that is instantiated by the containing object element.
8/22/12
XA M L Syntax In Detail
used as the value. In many cases the object returned by a markup extension will be a reference to an existing object, or an expression that defers evaluation until run time, and is not a newly instantiated object. 2. If the property is declared with an attributed TypeConverter, or the value type of that property is declared with an attributed TypeConverter, the string value of the attribute is submitted to the type converter as a conversion input, and the converter will return a new object instance. 3. If there is no TypeConverter, a direct conversion to the property type is attempted. This final level is a direct conversion at the parser-native value between XAML language primitive types, or a check for the names of named constants in an enumeration (the parser then accesses the matching values).
<-Ti wl ntcmie bcueVsblt i ntafaws eueain-> !-hs il o opl, eas iiiiy s o lgie nmrto... . <rge Poet=Vsblt"Vle"olpe,idn> Tigr rpry"iiiiy au=ClasdHde" <etr../ Ste . > <Tigr /rge> .. . Flagwise enumerations that support attributes that are settable in XAML are rare in WPF. However, one such enumeration is StyleSimulations. You could, for instance, use the comma-delimited flagwise attribute syntax to modify the example provided in the Remarks for the Glyphs class; S y e i u a i n = " o d i u a i n could become tlSmltos BlSmlto" S y e i u a i n = " o d i u a i n I a i S m l t o " KeyBinding.Modifiers is tlSmltos BlSmlto,tlciuain. another property where more than one enumeration value can be specified. However, this property happens to be a special case, because the ModifierKeys enumeration supports its own type converter. The type converter for modifiers uses a plus sign (+) as a delimiter rather than a comma (,). This conversion supports the more traditional syntax to represent key combinations in Microsoft Windows programming, such as "Ctrl+Alt".
8/22/12
XA M L Syntax In Detail
Or, you can reference an attached property or attached event, independent of the containing object element. (Attached properties are discussed in an upcoming section.) You can also name any event from any object that is accessible through the default namespace by using a typeName.event partially qualified name; this syntax supports attaching handlers for routed events where the handler is intended to handle events routing from child elements, but the parent element does not also have that event in its members table. This syntax resembles an attached event syntax, but the event here is not a true attached event. Instead, you are referencing an event with a qualified name. For more information, see Routed Events Overview. For some scenarios, property names are sometimes provided as the value of an attribute, rather than the attribute name. That property name can also include qualifiers, such as the property specified in the form ownerType.dependencyPropertyName. This scenario is common when writing styles or templates in XAML. The processing rules for property names provided as an attribute value are different, and are governed by the type of the property being set or by the behaviors of particular WPF subsystems. For details, see Styling and Templating. Another usage for property names is when an attribute value describes a property-property relationship. This feature is used for data binding and for storyboard targets, and is enabled by the PropertyPath class and its type converter. For a more complete description of the lookup semantics, see PropertyPath XAML Syntax.
8/22/12
XA M L Syntax In Detail
use a simpler attribute syntax. One scenario for filling a property element with a string is for properties that are not the XAML content property but still are used for representation of UI text, and particular whitespace elements such as linefeeds are required to appear in that UI text. Attribute syntax cannot preserve linefeeds, but property element syntax can, so long as significant whitespace preservation is active (for details, see Whitespace Processing in XAML). Another scenario is so that x:Uid Directive can be applied to the property element and thus mark the value within as a value that should be localized in the WPF output BAML or by other techniques. A property element is not represented in the WPF logical tree. A property element is just a particular syntax for setting a property, and is not an element that has an instance or object backing it. (For details on the logical tree concept, see Trees in WPF.) For properties where both attribute and property element syntax are supported, the two syntaxes generally have the same result, although subtleties such as whitespace handling can vary slightly between syntaxes.
Collection Syntax
The XAML specification requires XAML processor implementations to identify properties where the value type is a collection. The general XAML processor implementation in .NET is based on managed code and the CLR, and it identifies collection types through one of the following: Type implements IList. Type implements IDictionary. Type derives from Array (for more information about arrays in XAML, see x:Array Markup Extension.) If the type of a property is a collection, then the inferred collection type does not need to be specified in the markup as an object element. Instead, the elements that are intended to become the items in the collection are specified as one or more child elements of the property element. Each such item is evaluated to an object during loading and added to the collection by calling the Add method of the implied collection. For example, the Triggers property of Style takes the specialized collection type TriggerCollection, which implements IList. It is not necessary to instantiate a TriggerCollection object element in the markup. Instead, you specify one or more Trigger items as elements within the S y e T i g r property element, where Trigger (or a tl.rges derived class) is the type expected as the item type for the strongly typed and implicit TriggerCollection. XAML <tl xKy"pcaBto"TreTp={:yeBto}> Sye :e=Seilutn agtye"xTp utn" <tl.rges SyeTigr> <rge Poet=Bto.sosOe"Vle"re> Tigr rpry"utnIMuevr au=tu" <etrPoet ="akrud Vle"e"> Ste rpry Bcgon" au=Rd/ <Tigr /rge> <rge Poet=Bto.srse"Vle"re> Tigr rpry"utnIPesd au=tu" <etrPoet ="oerud Vle"re"> Ste rpry Frgon" au=Gen/ <Tigr /rge> <SyeTigr> /tl.rges <Sye /tl> A property may be both a collection type and the XAML content property for that type and derived types, which is discussed in the next section of this topic. An implicit collection element creates a member in the logical tree representation, even though it does not appear in the markup as an element. Usually the constructor of the parent type
msdn.microsoft.com/en-us/library/ms788723 6/13
8/22/12
XA M L Syntax In Detail
performs the instantiation for the collection that is one of its properties, and the initially empty collection becomes part of the object tree.
Note
The generic list and dictionary interfaces (IList(Of T) and IDictionary(Of TKey, TValue)) are not supported for collection detection. However, you can use the List(Of T) class as a base class, because it implements IList directly, or Dictionary(Of TKey, TValue) as a base class, because it implements IDictionary directly. In the .NET Reference pages for collection types, this syntax with the deliberate omission of the object element for a collection is occasionally noted in the XAML syntax sections as Implicit Collection Syntax. With the exception of the root element, every object element in a XAML file that is nested as a child element of another element is really an element that is one or both of the following cases: a member of an implicit collection property of its parent element, or an element that specifies the value of the XAML content property for the parent element (XAML content properties will be discussed in an upcoming section). In other words, the relationship of parent elements and child elements in a markup page is really a single object at the root, and every object element beneath the root is either a single instance that provides a property value of the parent, or one of the items within a collection that is also a collection-type property value of the parent. This singleroot concept is common with XML, and is frequently reinforced in the behavior of APIs that load XAML such as Load. The following example is a syntax with the object element for a collection (GradientStopCollection) specified explicitly. XAML <ierrdetrs> LnaGainBuh <ierrdetrs.rdettp> LnaGainBuhGainSos <rdettpolcin GainSoCleto> <rdettpOfe=00 Clr"e"/ GainSo fst"." oo=Rd > <rdettpOfe=10 Clr"le / GainSo fst"." oo=Bu" > <GainSoCleto> /rdettpolcin <LnaGainBuhGainSos /ierrdetrs.rdettp> <LnaGainBuh /ierrdetrs> Note that it is not always possible to explicitly declare the collection. For instance, attempting to declare TriggerCollection explicitly in the previously shown Triggers example would fail. Explicitly declaring the collection requires that the collection class must support a default constructor, and TriggerCollection does not have a default constructor.
8/22/12
XA M L Syntax In Detail
directly. Property element tags for other properties on an element are not assigned as "content" per a strict XAML language definition; they are processed previously in the XAML parser's processing order and are not considered to be "content".
<utnIa a Bto> m <utnBcgon>le/utnBcgon> Bto.akrudBu<Bto.akrud bu bto<Bto> le utn/utn This is illegal essentially because if this syntax were made explicit by using property element syntax for the content property, then the content property would be set twice:
<utn Bto> <utnCnetIa a<Bto.otn> Bto.otn> m /utnCnet <utnBcgon>le/utnBcgon> Bto.akrudBu<Bto.akrud <utnCnet bu bto<Bto.otn> Bto.otn> le utn/utnCnet <Bto> /utn A similarly illegal example is if the content property is a collection, and child elements are interspersed with property elements:
<tcPnl Sakae> <utnTi eape/utn Bto>hs xml<Bto> <tcPnlRsucs Sakae.eore> <oiClrrs xKy"lers"Clr"le/ SldooBuh :e=BuBuh oo=Bu"> <Sakae.eore> /tcPnlRsucs <utn..i ilglXM<Bto> Bto>. s lea AL/utn <Sakae> /tcPnl
8/22/12
XA M L Syntax In Detail
xlsx"tp/shmsmcootcmwnx20/al mn:=ht:/cea.irsf.o/if/06xm" > <tcPnl Sakae> <utnBto 1/utn Bto>utn <Bto> <utnBto 2/utn Bto>utn <Bto> <utnBto 3/utn Bto>utn <Bto> <Sakae> /tcPnl <Pg> /ae Note that neither the property element for Children nor the element for the UIElementCollection is required in the markup. This is a design feature of XAML so that recursively contained elements that define a UI are more intuitively represented as a tree of nested elements with immediate parent-child element relationships, without intervening property element tags or collection objects. In fact, UIElementCollection cannot be specified explicitly in markup as an object element, by design. Because its only intended use is as an implicit collection, UIElementCollection does not expose a public default constructor and thus cannot be instantiated as an object element.
Mixing Property Elements and Object Elements in an Object with a Content Property
The XAML specification declares that a XAML processor can enforce that object elements that are used to fill the XAML content property within an object element must be contiguous, and must not be mixed. This restriction against mixing property elements and content is enforced by the WPF XAML processors. You can have a child object element as the first immediate markup within an object element. Then you can introduce property elements. Or, you can specify one or more property elements, then content, then more property elements. But once a property element follows content, you cannot introduce any further content, you can only add property elements. This content / property element order requirement does not apply to inner text used as content. However, it is still a good markup style to keep inner text contiguous, because significant whitespace will be difficult to detect visually in the markup if property elements are interspersed with inner text.
XAML Namespaces
None of the preceding syntax examples specified a XAML namespace other than the default XAML namespace. In typical WPF applications, the default XAML namespace is specified to be the WPF namespace. You can specify XAML namespaces other than the default XAML namespace and still use similar syntax. But then, anywhere where a class is named that is not accessible within the default XAML namespace, that class name must be preceded with the prefix of the XAML namespace as mapped to the corresponding CLR namespace. For example, < u t m E a p e >is cso:xml/ object element syntax to instantiate an instance of the E a p eclass, where the CLR xml namespace containing that class (and possibly the external assembly information that contains backing types) was previously mapped to the c s o prefix. utm For more information about XAML namespaces, see XAML Namespaces and Namespace Mapping for WPF XAML.
Markup Extensions
XAML defines a markup extension programming entity that enables an escape from the normal
msdn.microsoft.com/en-us/library/ms788723 9/13
8/22/12
XA M L Syntax In Detail
XAML processor handling of string attribute values or object elements, and defers the processing to a backing class. The character that identifies a markup extension to a XAML processor when using attribute syntax is the opening curly brace ({), followed by any character other than a closing curly brace (}). The first string following the opening curly brace must reference the class that provides the particular extension behavior, where the reference may omit the substring "Extension" if that substring is part of the true class name. Thereafter, a single space may appear, and then each succeeding character is used as input by the extension implementation, up until the closing curly brace is encountered. The .NET XAML implementation uses the MarkupExtension abstract class as the basis for all of the markup extensions supported by WPF as well as other frameworks or technologies. The markup extensions that WPF specifically implements are often intended to provide a means to reference other existing objects, or to make deferred references to objects that will be evaluated at run time. For example, a simple WPF data binding is accomplished by specifying the { i d n } Bnig markup extension in place of the value that a particular property would ordinarily take. Many of the WPF markup extensions enable an attribute syntax for properties where an attribute syntax would not otherwise be possible. For example, a Style object is a relatively complex type that contains a nested series of objects and properties. Styles in WPF are typically defined as a resource in a ResourceDictionary, and then referenced through one of the two WPF markup extensions that request a resource. The markup extension defers the evaluation of the property value to a resource lookup and enables providing the value of the Style property, taking type Style, in attribute syntax as in the following example:
Attached Properties
Attached properties are a programming concept introduced in XAML whereby properties can be owned and defined by a particular type, but set as attributes or property elements on any element. The primary scenario that attached properties are intended for is to enable child elements in a markup structure to report information to a parent element without requiring an extensively shared object model across all elements. Conversely, attached properties can be used by parent elements to report information to child elements. For more information on the purpose of attached properties and how to create your own attached properties, see Attached Properties Overview. Attached properties use a syntax that superficially resembles property element syntax, in that you also specify a typeName.propertyName combination. There are two important differences: You can use the typeName.propertyName combination even when setting an attached property through attribute syntax. Attached properties are the only case where qualifying the property name is a requirement in an attribute syntax. You can also use property element syntax for attached properties. However, for typical property element syntax, the typeName you specify is the object element that contains the property element. If you are referring to an attached property, then the typeName is the class that defines the attached property, not the containing object element.
msdn.microsoft.com/en-us/library/ms788723 10/13
8/22/12
XA M L Syntax In Detail
Attached Events
Attached events are another programming concept introduced in XAML where events can be defined by a specific type, but handlers may be attached on any object element. In the WOF implementation, often the type that defines an attached event is a static type that defines a service, and sometimes those attached events are exposed by a routed event alias in types that expose the service. Handlers for attached events are specified through attribute syntax. As with attached events, the attribute syntax is expanded for attached events to allow a typeName.eventName usage, where typeName is the class that provides Add and Remove event handler accessors for the attached event infrastructure, and eventName is the event name.
<ae Pg
xlsx"tp/shmsmcootcmwnx20/al mn:=ht:/cea.irsf.o/if/06xm"
The XAML language XAML namespace The partial class declaration that connects markup to any codebehind defined for the partial class End of object element for the root. Object is not closed yet because the element contains child elements
xCas"xmlNmsaeEapeoe :ls=Eapeaepc.xmlCd"
>
8/22/12
XA M L Syntax In Detail
but that produce verbosity or other aesthetic issues that interfere with XAML files remaining human-readable when your develop applications that contain XAML sources.
B t o . a k r u dworks because the qualified lookup for that property on Button is utnBcgon
successful (Background was inherited from Control) and Button is the class of the object element or a base class. C n r l B c g o n works because the Control class actually oto.akrud defines Background and Control is a Button base class. However, the following typeName.memberName form example does not work and is thus shown commented: XAML <-<utnLblBcgon=Bu"De ntwr<Bto>-> !-Bto ae.akrud"le>os o ok/utn Label is another derived class of Control, and if you had specified L b l B c g o n within a ae.akrud Label object element, this usage would have worked. However, because Label is not the class or base class of Button, the specified XAML processor behavior is to then process L b l B c g o n as an attached property. L b l B c g o n is not an available attached ae.akrud ae.akrud property, and this usage fails.
8/22/12
<oto.akrud CnrlBcgon> <ierrdetrs Saton=00 Edon=11> LnaGainBuh trPit"," nPit"," <rdettpClr"elw Ofe=00 / GainSo oo=Ylo" fst"." > <rdettpClr"iere"Ofe=10 / GainSo oo=LmGen fst"." > <LnaGainBuh /ierrdetrs> <CnrlBcgon> /oto.akrud <Bto> /utn Here, the property element was given as C n r l B c g o n even though the property oto.akrud element was contained in B t o . utn But just like typeName.memberName form for attributes, baseTypeName.memberName is poor style in markup, and you should avoid it.
XA M L Syntax In Detail
See Also
Concepts XAML Overview (WPF) Dependency Properties Overview TypeConverters and XAML XAML and Custom Classes for WPF Other Resources XAML Namespace (x:) Language Features WPF XAML Extensions
Yes
No
msdn.microsoft.com/en-us/library/ms788723
13/13