FrameworkElement.Name 属性

定义

获取或设置元素的标识名称。Gets or sets the identifying name of the element. 该名称提供引用,以便代码隐藏(如事件处理程序代码)可以引用标记元素(在 XAMLXAML 处理器的处理过程中构造该元素之后)。The name provides a reference so that code-behind, such as event handler code, can refer to a markup element after it is constructed during processing by a XAMLXAML processor.

public:
 property System::String ^ Name { System::String ^ get(); void set(System::String ^ value); };
[System.Windows.Localizability(System.Windows.LocalizationCategory.NeverLocalize)]
public string Name { get; set; }
member this.Name : string with get, set
Public Property Name As String

属性值

元素的名称。The name of the element. 默认值为一个空字符串。The default is an empty string.

实现

属性

示例

下面的示例在代码中设置 Name 属性,然后通过调用 RegisterName将该名称注册到新创建的 NameScope 中。The following example sets the Name property in code, and then registers the name into the newly created NameScope by calling RegisterName. 此处所示的技术是使用情节提要进行动画处理的一项要求,因为情节提要需要 Name目标,并且不能以对象引用为目标。The technique illustrated here is a requirement for animating with storyboards, because storyboards require targeting by the Name, and cannot be targeted by object reference.

//  
// Create a Rectangle
//
Rectangle myRectangle = new Rectangle();
myRectangle.Width = 200;
myRectangle.Height = 200;
myRectangle.Name = "myRectangle";
this.RegisterName(myRectangle.Name, myRectangle); 
'  
' Create a Rectangle
'
Dim myRectangle As New Rectangle()
myRectangle.Width = 200
myRectangle.Height = 200
myRectangle.Name = "myRectangle"
Me.RegisterName(myRectangle.Name, myRectangle)

注解

此属性最常见的用法是将 XAMLXAML 元素名称指定为标记中的属性。The most common usage of this property is to specify a XAMLXAML element name as an attribute in markup.

此属性实质上提供了一个 WPF 框架级别的方便属性来设置 XAMLXAML X:Name 指令This property essentially provides a WPF framework-level convenience property to set the XAMLXAML x:Name Directive.

名称在名称范围内必须是唯一的。Names must be unique within a namescope. 有关详细信息,请参阅 WPF XAML NamescopesFor more information, see WPF XAML Namescopes.

如果你在代码中创建元素不常用,则获取 NameGetting a Name if you are creating elements in code is not common. 如果已在代码中正确引用,则只需调用元素引用的方法和属性,通常不需要 NameIf you have the appropriate reference in code already, you can just call methods and properties on the element reference and will not generally need the Name. 这种情况的一个例外是,如果 Name 字符串具有一些重载含义(例如,如果在 UIUI中显示该名称很有用)。An exception to this is if the Name string has some overloaded meaning, for instance if it is useful to display that name in UIUI. 如果也不建议从标记设置原始 Name,则从代码隐藏设置 Name,并且在加载 XAMLXAML 后更改属性将不会更改原始对象引用。Setting a Name from code-behind if the original Name was set from markup is also not recommended, and changing the property after loading the XAMLXAML will not change the original object reference. 仅当在分析期间显式创建了基础名称范围时,才会创建对象引用。The object references are created only when the underlying namescopes are explicitly created during parsing. 必须专门调用 RegisterName,才能对已加载元素的 Name 属性进行有效更改。You must specifically call RegisterName to make an effective change to the Name property of an already loaded element.

从代码中设置 Name 很重要的一种情况是,为演示图板将针对其运行的元素注册名称,以便可以在运行时引用它们。One notable case where setting Name from code is important is when registering names for elements that storyboards will run against, so that they can be referenced at run time. 注册名称之前,可能还需要实例化并分配一个 NameScope 实例。Before you can register a name, might also need to instantiate and assign a NameScope instance. 请参阅 "示例" 部分或情节提要概述See the Example section, or Storyboards Overview.

从代码设置 Name 具有有限的应用程序,但通过 Name 获取元素更常见。Setting Name from code has limited applications, but getting an element by Name is more common. 一种特殊情况是,如果您的应用程序支持将页面重新加载到应用程序中的导航模型,并且运行时代码不一定要为该页定义代码隐藏。One particular scenario is if your application supports a navigation model where pages reload into the application, and the run time code is not necessarily code-behind defined for that page. 实用工具方法 FindName(可从任何 FrameworkElement获取)可以在该元素的逻辑树中 Name 来查找任何元素,并根据需要递归搜索树。The utility method FindName, which is available from any FrameworkElement, can find any element by Name in the logical tree for that element, searching the tree recursively as necessary. 或者,您可以使用 LogicalTreeHelperFindLogicalNode 静态方法,该方法也采用 Name 字符串作为参数。Or you can use the FindLogicalNode static method of LogicalTreeHelper, which also takes a Name string as an argument.

通常使用的根元素(例如Window)实现接口 PageINameScopeTypically used root elements (Window, Page for example) implement the interface INameScope. 此接口的实现应强制要求名称在其范围内明确。Implementations of this interface are expected to enforce that names be unambiguous within their scope. 定义此接口的根元素还定义所有相关 Api 的名称范围行为边界。The root elements that define this interface also define the namescope behavior boundaries for all the related APIs.

Name 属性还作为其他进程的标识符。The Name property also serves as an identifier for other processes. 例如,WPFWPF 自动化模型将使用与客户端和提供程序的 AutomationId NameFor instance, the WPFWPF automation model will use Name as the AutomationId for clients and providers.

用于 Name 的字符串值具有某些限制,这是由 XAMLXAML 规范定义的基础X:Name 指令施加的。The string values used for Name have some restrictions, as imposed by the underlying x:Name Directive defined by the XAMLXAML specification. 最值得注意的是,Name 必须以字母或下划线字符()开头,并且必须仅包含字母、数字或下划线。Most notably, a Name must start with a letter or the underscore character (), and must contain only letters, digits, or underscores. 有关详细信息,请参阅 WPF XAML NamescopesFor more information, see WPF XAML Namescopes.

Name 是不能进行动画处理的几个依赖项属性之一(IsAnimationProhibited 在元数据中 true),因为该名称本身对于目标为动画是至关重要的。Name is one of the very few dependency properties that cannot be animated (IsAnimationProhibited is true in metadata), because the name itself is vital for targeting an animation. 数据绑定 Name 在技术上是可行的,但这种情况非常少见,因为数据绑定 Name 无法提供属性的主要预期用途:为代码隐藏提供标识符连接点。Data binding a Name is technically possible, but is an extremely uncommon scenario because a data-bound Name cannot serve the main intended purpose of the property: to provide an identifier connection point for code-behind.

依赖项属性信息Dependency Property Information

标识符字段Identifier field NameProperty
元数据属性设置为 trueMetadata properties set to true IsAnimationProhibited

适用于