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屬性中的程式碼,然後註冊到新建立的 名稱NameScope藉由呼叫RegisterNameThe 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字串具有某些多載意義,例如它是否可顯示該名稱在UIUIAn 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. 或者您可以使用FindLogicalNode靜態方法LogicalTreeHelper,也會採用Name做為引數的字串。Or you can use the FindLogicalNode static method of LogicalTreeHelper, which also takes a Name string as an argument.

常用根項目 (WindowPage比方說) 實作介面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. 定義這個介面的根項目也定義名稱範圍行為界限的所有相關APIAPIsThe 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 automation 模型會使用Name為用戶端和提供者的 AutomationId。For instance, the WPFWPF automation model will use Name as the AutomationId for clients and providers.

所用的字串值Name有一些限制,如基礎所加諸X:name 指示詞所定義XAMLXAML規格。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 是其中一個非常少無法變成動畫的相依性屬性 (IsAnimationProhibitedtrue中繼資料中),因為名稱本身是針對動畫很重要。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

適用於