Export (0) Print
Expand All

FrameworkContentElement.Name Property

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.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

[LocalizabilityAttribute(LocalizationCategory.NeverLocalize)]
public string Name { get; set; }
<object Name="string" .../>

Property Value

Type: System.String
The name of the element.

Implements

IFrameworkInputElement.Name

Identifier field

NameProperty

Metadata properties set to true

IsAnimationProhibited

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.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft