FrameworkElement.Name FrameworkElement.Name FrameworkElement.Name FrameworkElement.Name Property

定义

获取或设置元素的标识名称。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.

Name如果你在代码中创建元素不常用, 则获取。Getting 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的, 则XAMLXAML 从代码隐藏中设置,将不会更改原始对象引用。NameSetting 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. 您也可以使用FindLogicalNode的静态LogicalTreeHelper方法Name , 该方法也会将字符串作为参数。Or you can use the FindLogicalNode static method of LogicalTreeHelper, which also takes a Name string as an argument.

通常使用的根元素Window( Page例如) 实现接口INameScopeTypically 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. 定义此接口的根元素还为所有相关APIAPIs的定义了名称范围行为边界。The root elements that define this interface also define the namescope behavior boundaries for all the related APIAPIs.

Name属性还可用作其他进程的标识符。The Name property also serves as an identifier for other processes. 例如, WPFWPF自动化模型Name将用作客户端和提供程序的 AutomationId。For 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

适用于