Export (0) Print
Expand All

x:Static Markup Extension

References any static by-value code entity defined in a Common Language Specification (CLS) compliant way The property referenced is evaluated prior to loading the remainder of the XAML page and can be used to provide the value of a property in XAML.

<object property="{x:Static prefix:typeName.staticMemberName}" .../>

<object>
  <object.property>
    <x:Static Member="prefix:typeName.staticMemberName" .../>
  </object.property>
</object>

prefix

Optional. A prefix that maps a non-default xmlns namespace. See Remarks.

typeName

Required. The type that defines the desired static member.

staticMemberName

Required. The name of the desired static value member (a constant, a static property, a field, or an enumeration value).

The code entity referenced must be one of the following:

  • constant

  • static property

  • field

  • enumeration value

Specifying any other code entity, such as a nonstatic property, results in a compile time error.

x:Static references can be made to static fields or properties that are not within the default xmlns namespace, but this requires a prefix mapping. You define the default xmlns namespace for any given XAML element as an attribute. Generally, this is defined on the root element so that the xmlns applies to all elements below the root. Generally, the default xmlns namespace you use for Windows Presentation Foundation (WPF) programming is the WPF XAML namespace. You must map a prefix if one of the following is true:

  • You are referencing a type that exists in Microsoft .NET Framework version 3.0 but is not part of the WPF xmlns namespace. This is a fairly common scenario for using x:Static. For example, you might use an x:Static reference with a prefix mapping to the System namespace in order to reference the static properties of the Environment class.

  • You are referencing a type from a custom assembly.

  • You are referencing a type that exists in a WPF assembly, but that type is within a CLR namespace that was not xmlns mapped to be part of the WPF xmlns definition. The mapping is performed by definitions in that assembly. Nonmapped CLR namespaces are typical for WPF CLR namespaces that are not generally intended for XAML, such as System.Windows.Threading.

For more information on prefixes and xmlns namespaces, see XAML Namespaces and Namespace Mapping.

You can use x:Static references that are not directly the type of a property's value, but can be evaluated to that type. For instance, you can use an x:Static reference to obtain a value from an enumeration, or out of a static property such as the various colors and brushes defined by SystemColors. x:Static references can potentially be used to set any property in XAML syntax, even those that may actually be based on a property with a reference type, because the actual handling of the x:Static value once evaluated might vary, depending on type converter behaviors of the property where the static value is applied.

Attribute syntax is the most common syntax used with this markup extension. The string token provided after the x:Static identifier string is assigned as the Member value of the underlying StaticExtension extension class.

x:Static can be used in object element syntax. In this case, specifying the value of the Member property is required.

x:Static can also be used in a verbose attribute usage that specifies the Member property as a property=value pair:

<object property="{x:Static Member=prefix:typeName.staticMemberName}" .../>

The verbose usage is often useful for extensions that have more than one settable property, or if some properties are optional. Because x:Static has only one settable property, which is required, this verbose usage is not typical.

In the WPF XAML reader implementation, the handling for this markup extension is defined by the StaticExtension class.

x:Static is a markup extension. Markup extensions are typically implemented when there is a requirement to escape attribute values to be other than literal values or handler names, and the requirement is more global than just putting type converters on certain types or properties. All markup extensions in XAML use the { and } characters in their attribute syntax, which is the convention by which a XAML reader recognizes that a markup extension must process the attribute. For details on markup extensions in general, see Markup Extensions and XAML.

Community Additions

ADD
Show:
© 2014 Microsoft