Name Property (DependencyObject)

Gets or sets a unique identification for the object. A Name can only be set from initial parsing of XAML, it is not settable in scripting.

XAML
<object Name="string" .../>
-or-
<object x:Name="string" .../>
Scripting
value = object.Name

Property Value

string

A string representing the name of the object.

This property is read-only for scripting, write-only for XAML.

Remarks

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 where the name came either from a Name attribute or from 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, even though 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 only understood 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 UI definition will possess 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 UI definition for related XAML-consuming technologies (such as WPF).
  • Markup attributes are by nature write-only, unless you are examining the markup with a dedicated XML 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 wish.

The reason why you set Name or x:Name in XAML is so that you can easily obtain a runtime reference to the object that was created by parsing the XAML. In order to get that reference, simply use the return value of the FindName method, specifying the name of the object you want to retrieve as an input parameter. FindName works from almost any scope and will search the entire runtime set of objects; you are not limited to searching in particular parent-child axes (although if you create discrete namescopes with CreateFromXaml, that might complicate your FindName usages; see XAML Namescopes).

The string that can be used for Name is slightly constained by a grammar that is defined by the XAML language and influenced by other parsing considerations. For instance, you cannot use an ampersand (&) in a XAML name, because that triggers the entity escape sequence in XML. For best results, use numbers, letters, and the underscore character only. For details, see XamlName Grammar.

Some of the object types listed in Applies To sections in this SDK reference are abstract types. Therefore, this property cannot be retrieved or set on that type in JavaScript because there is no way to obtain instances of abstract types in JavaScript programming.

Applies To

Accessibility, ArcSegment, BeginStoryboard, BezierSegment, Boolean, Brush, Canvas, Color, ColorAnimation, ColorAnimationUsingKeyFrames, ColorKeyFrame, ColorKeyFrameCollection, DependencyObject, DiscreteColorKeyFrame, DiscreteDoubleKeyFrame, DiscretePointKeyFrame, DoubleAnimation, DoubleAnimationUsingKeyFrames, DoubleKeyFrame, DoubleKeyFrameCollection, Downloader, DrawingAttributes, Duration, Ellipse, EllipseGeometry, EventTrigger, Geometry, GeometryCollection, GeometryGroup, Glyphs, GradientStop, GradientStopCollection, Image, ImageBrush, InkPresenter, Inlines, KeySpline, KeyTime, Line, LinearColorKeyFrame, LinearDoubleKeyFrame, LinearGradientBrush, LinearPointKeyFrame, LineBreak, LineGeometry, LineSegment, Matrix, MatrixTransform, MediaAttribute, MediaAttributeCollection, MediaElement, Path, PathFigure, PathFigureCollection, PathGeometry, PathSegment, PathSegmentCollection, Point, PointAnimation, PointAnimationUsingKeyFrames, PointKeyFrame, PointKeyFrameCollection, PolyBezierSegment, Polygon, Polyline, PolyLineSegment, PolyQuadraticBezierSegment, QuadraticBezierSegment, RadialGradientBrush, Rect, Rectangle, RectangleGeometry, RepeatBehavior, ResourceDictionary, RotateTransform, Run, RuntimeErrorEventArgs, ScaleTransform, SkewTransform, SolidColorBrush, SplineColorKeyFrame, SplineDoubleKeyFrame, SplinePointKeyFrame, Storyboard, Stretch, Stroke, StrokeCollection, StylusInfo, StylusPoint, StylusPointCollection, TextBlock, Timeline, TimelineCollection, TimelineMarker, TimelineMarkerCollection, TimelineMarkerEventArgs, TimeSpan, TransformCollection, TransformGroup, TranslateTransform, TriggerActionCollection, TriggerCollection, VisualCollection, VideoBrush

See Also

XAML Namescopes
Using the CreateFromXaml Method
Referencing and Modifying Silverlight Objects
XAML Syntax Overview
FindName