次の方法で共有


x:Name Attribute

Uniquely identifies object elements for purpose of access to the instantiated element from code-behind or general code. An established x:Name can be considered equivalent to the variable holding an object reference, as returned by a constructor.

XAML Attribute Usage

<object x:Name="XAMLNameValue".../>

XAML Values

XAMLNameValue

A string that conforms to the restrictions of the XAMLName Grammar.

Remarks

The specified x:Name becomes the name of a field that is created in the underlying code when XAML is loaded or compiled, and that field holds a reference to the object.

By default, the created field is internal. You can change field access by specifying the x:FieldModifier Attribute.

For an application that uses the Microsoft Visual Basic .NET targets and includes XAML files, a separate reference property is created during compilation that adds the WithEvents keyword to all elements that have an x:Name, to support Handles syntax for event handler delegates. This property is always public. For details, see Visual Basic and WPF Event Handling.

x:Name must be unique within a name scope. In most common XAML cases, the namescope is defined by those elements contained in a single XAML page.

x:Name is used by the XAML loader to register a name into a namescope, even for cases where the page is not compiled (for instance, loose XAML). This is because the x:Name is potentially needed for ElementName binding. For details, see Data Binding Overview.

x:Name cannot be applied in certain scopes. For instance, items in a ResourceDictionary cannot have names, because they already have the x:Key Attribute as their unique identifier.

Rules for permittance of x:Name as well as the uniqueness scoping are defined by the underlying Windows Presentation Foundation (WPF) framework implementation that divides various markup elements into separate NameScope ranges, such as resource dictionaries, the logical element tree, etc.

Some WPF framework applications may be able to avoid any use of the x:Name attribute, because the Name dependency property as specified within the WPF namespace for several of the important base classes such as FrameworkElement/FrameworkContentElement satisfies this same purpose. There are still some common XAML and framework scenarios where programmatic access to an element with no Name property exist, most notably in certain graphics support classes. For instance, you should specify x:Name on timelines and transforms created in XAML, if you intend to reference them from procedural code.

If Name is defined as a property on an element, Name and x:Name can be used interchangeably, but an error will result if both are specified on the same element.

For custom elements, the property (can be either a CLR property or dependency property) that maps to x:Name on any given object can be established or changed by designating that property with the RuntimeNamePropertyAttribute in the property declaration code.

Name can be set using XAML attribute syntax, using XAML property element syntax, and in procedural code using SetValue; note however that setting the Name property programmatically does not create the representative field reference within the namescope in some circumstances. . x:Name cannot be set in XAML property element syntax, or in procedural code using SetValue; it can only be set using attribute syntax on elements.

Example Attribute Usage

<object x:Name="aStringNameValue".../>

See Also

Reference

Name
Name

Concepts

Element Tree