Sdílet prostřednictvím


x:Static Markup Extension

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

XAML Attribute Usage

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

XAML Object Element Usage

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

XAML Values

prefix

Optional. A prefix that refers to a mapped, non-default XAML 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).

Remarks

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 XAML namespace for the current XAML document, but this requires a prefix mapping. You define the default XAML namespace for any given XAML element as an attribute. Generally, this is defined on the root element of the XAML document so that the XAML namespace applies to all elements below the root, and thus the whole XAML document. Generally, the default XAML namespace you use for Windows Presentation Foundation (WPF) programming is the WPF namespace. You must map a prefix for a non-default XAML namespace if one of the following is true:

  • You are referencing a type that exists in Microsoft .NET Framework but is not part of the default WPF XAML 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 CLR namespace in order to reference the static properties of the Environment class. But in order to do so, you must create and prefix-map a non-default XAML namespace to System CLR namespace and mscorlib assembly, because that namespace-assembly combination is not part of the default XAML namespace for WPF.

  • You are referencing a type from a custom assembly. In this case, you must prefix and declare a non-default XAML namespace based on your own assembly name and CLR namespace information.

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

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

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.

Use caution when making x:Static references that are not directly the type of a property's value. In the XAML processing sequence for attribute values, markup extension provided values do not invoke further value conversion, even if your x:Static reference results in a text string, and a value conversion for attribute values normally takes place either for that specific member or for any member values of the return type.

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 uncommon.

In the WPF XAML processor 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 processor recognizes that a markup extension must process the attribute. For details on markup extensions in general, see Markup Extensions and XAML.

See Also

Concepts

Styling and Templating

XAML Overview

Markup Extensions and XAML

Reference

x:Type Markup Extension