Microsoft Silverlight will reach end of support after October 2021. Learn more.
Gets or sets a unique identification for the object. A Name can be set only during the initial parsing of XAML; it is not settable in scripting.
<object Name="namestring" .../> -or- <object x:Name="namestring" .../>
value = object.Name
A string that represents the name of the object.
This property is read-only for scripting, write-only for XAML.
A string that conforms to the XamlName Grammar.
The Name property is read-only for scripting. The only permitted set mechanism is as an attribute in XAML markup. The Name property can be used to get names that come from either a Name attribute or an x:Name attribute value in markup. Certain Silverlight objects (for example Inlines) cannot be created in XAML as object elements, which means that there is no way to set a Name. Nevertheless, the Name property is available in the object model for such objects in order to support a common object model surface for DependencyObject, although it will always return an empty string.
The Name property is generally equivalent to the x:Name attribute specified in XAML. Name and x:Name differ in the following aspects:
x:Name as an attribute is understood by all XAML readers. Name as an attribute in the default xmlns is understood only if it maps to an existing property on the element where it is declared. In the case of DependencyObject.Name, all common elements used for user interface definition will have the Name property/attribute through DependencyObject. DependencyObject provides core support for all Silverlight classes that can be declared as object elements in XAML, so this XAML language versus XAML implementation distinction is not relevant for Silverlight XAML. However, you should keep this distinction in mind if you are importing XAML that may have served as the user interface definition for related XAML-consuming technologies (such as Windows Presentation Foundation).
Markup attributes are by nature write-only, unless you are examining the markup with a dedicated XML Document Object Model (DOM), so the only operation you can perform with x:Name is to set the attribute value.
Using x:Name will require that you map the XAML xmlns to the x: prefix. This is typically done initially in most XAML files that are generated by tools, templates, or visual editors. The prefix x: is used by convention for the XAML xmlns; you can choose a different prefix that you will map to the XAML xmlns if you want.
You set Name or x:Name in XAML object definitions so you can easily obtain a run-time reference to the object that was created when the XAML was parsed. In order to get that reference, simply use the return value of the FindName method, specifying the name of the object that you want to retrieve as an input parameter. FindName works from almost any scope and will search the entire run-time set of objects; you are not limited to searching in particular parent-child axes.
The Name string is somewhat constrained by XAML grammar and influenced by other parsing considerations. For example, you cannot use an ampersand (&) in a XAML name, because it triggers the entity escape sequence in XML. For best results, use numbers, letters, and the underscore character only. For a more precise definition, see XamlName Grammar.
BitmapImage (Silverlight 2)
Border (Silverlight 2)
DeepZoomImageTileSource (Silverlight 2)
DiscreteObjectKeyFrame (Silverlight 2)
MultiScaleImage (Silverlight 2)
ObjectAnimationUsingKeyFrames (Silverlight 2)
ObjectKeyFrameCollection (Silverlight 2)
PasswordBox (Silverlight 2)
Popup (Silverlight 2)
StackPanel (Silverlight 2)
TextBox (Silverlight 2)