Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FrameworkElement.Name Property

Gets or sets the identifying name of the element. 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 parsing by a XAML reader. This is a dependency property.

Namespace: System.Windows
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:

public string Name { get; set; }
/** @property */
public final String get_Name ()

/** @property */
public final void set_Name (String value)

public final function get Name () : String

public final function set Name (value : String)

<object Name="string" .../>

Property Value

The name of the element. The default is an empty string.

Identifier field


Metadata properties set to true


The most common usage of this property is to specify a XAML element name as an attribute in markup.

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

Names must be unique within a namescope. For more information, see WPF Namescopes.

Getting a Name if you are creating elements in code is not common. 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. An exception to this is if the Name string has some overloaded meaning, for instance if it is useful to display that name in UI. 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 XAML will not change the original object reference. The object references are created only when the underlying namescopes are explicitly created during parsing. You must specifically call RegisterName to make an effective change to the Name property of an already loaded element.

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. Before you can register a name, might also need to instantiate and assign a NameScope instance. See the Example section, or Animating with Storyboards Overview.

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. 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. Or you can use the FindLogicalNode static method of LogicalTreeHelper, which also takes a Name string as an argument.

Typically used root elements (Window, Page for example) implement the interface INameScope. Implementations of this interface are expected to enforce that names be unamibiguous within their scope. The root elements that define this interface also define the namescope behavior boundaries for all the related APIs.

The Name property also serves as an identifier for other processes. For instance, the WPF automation model will use Name as the AutomationId for clients and providers.

The string values used for Name have some restrictions, as imposed by the underlying x:Name Attribute defined by the XAML specification. Most notably, a Name must start with a letter or the underscore character (_), and must contain only letters, digits, or underscores. For more information, see WPF Namescopes.

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. Data binding a Name is technically possible, but is an extremely uncommon scenario because a databound Name cannot serve the main intended purpose of the property: to provide an identifier connection point for code-behind.

The following example sets the Name property in code, and then registers the name into the newly created NameScope by calling RegisterName. 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); 

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
© 2018 Microsoft