Редагувати

Поділитися через


FrameworkContentElement.Name Property

Definition

Gets or sets the identifying name of the element. The name provides an instance reference so that programmatic code-behind, such as event handler code, can refer to an element once it is constructed during parsing of XAML.

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; }
[<System.Windows.Localizability(System.Windows.LocalizationCategory.NeverLocalize)>]
member this.Name : string with get, set
Public Property Name As String

Property Value

The name of the element.

Implements

Attributes

Remarks

The most common usage of this property is when specifying a XAML element name in markup.

This property essentially provides a WPF framework-level convenience property to set the XAML x:Name Directive.

Getting a Name from code is not common, because if you have the appropriate reference in code already, you can just call methods and properties on the element reference and do not generally need the Name. An exception is if the string has some overloaded meaning, for instance if it is useful to display that name in UI. Setting a Name from code if the original Name was set from markup is also not recommended, and changing the property will not change the object reference. Such object references are created only when the underlying namescopes are explicitly created during XAML loading.

You must specifically call RegisterName to make an effective change on the Name property of an already loaded element.

One notable case where setting Name from code is important is for naming elements that storyboards will run against. Before you can register a name, might also need to instantiate and assign a NameScope instance. See the Example section, or Storyboards Overview.

Setting Name from code has limited applications, but looking up an element by name is more common, particularly if you are employing a navigation model where pages reload into the application and the run-time code is not necessarily the code-behind of that respective page. The utility method FindName, which is available from any FrameworkContentElement, can find any element by Name in that element's logical tree recursively. Or you can use the FindLogicalNode static method of LogicalTreeHelper, which also takes the Name string as an argument.

Commonly 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.

Dependency Property Information

Identifier field NameProperty
Metadata properties set to true IsAnimationProhibited

Applies to

See also