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. 如果您在程式碼中已經有適當的參考, 您可以只呼叫專案參考上的方法和屬性, 而且通常不Name會需要。If 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. XAMLXAML如果原始Name的是從標記設定的, 也不建議從程式碼後置設定,而且在載入之後變更屬性,將不會變更原始的物件參考。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可以使用的靜態方法, 它也會採用字串做為引數。NameOr 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 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案例, 因為資料系結無法提供屬性的主要用途: 為程式碼後置提供識別碼連接點。 NameData 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

適用於