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,將名稱註冊到新建立的 NameScopeThe 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 之後變更屬性,則從程式碼後置設定 Name,將不會變更原始的物件參考。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.

通常使用的根項目(例如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. 定義此介面的根項目也會定義所有相關 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 automation 模型將使用 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
設定為 true 的中繼資料屬性Metadata properties set to true IsAnimationProhibited

適用於