Partilhar via


Name (DependencyObject)

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

Property Value

Type: string

A string that represents the name of the object.

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

XAML Values

namestring

A string that conforms to the XamlName Grammar.

Managed Equivalent

FrameworkElement.Name or Inline.Name

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 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 concept of a XAML namescope controls the scope wherein a named object can be found through FindName. See Remarks in CreateFromXaml.

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.

Some of the object types listed in the "Applies To" sections of the Silverlight reference topics 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 Object (Silverlight JSAPI)

ArcSegment

BeginStoryboard

BezierSegment

BitmapImage (Silverlight 2)

Border (Silverlight 2)

Brush (abstract)

Canvas

Color

ColorAnimation

ColorAnimationUsingKeyFrames

ColorKeyFrame (abstract)

ColorKeyFrameCollection

DeepZoomImageTileSource (Silverlight 2)

DependencyObject (abstract)

DiscreteColorKeyFrame

DiscreteDoubleKeyFrame

DiscreteObjectKeyFrame (Silverlight 2)

DiscretePointKeyFrame

DoubleAnimation

DoubleAnimationUsingKeyFrames

DoubleKeyFrame (abstract)

DoubleKeyFrameCollection

Downloader

DrawingAttributes

Duration

Ellipse

EllipseGeometry

EventTrigger

Geometry (abstract)

GeometryCollection

GeometryGroup

Glyphs

GradientStop

GradientStopCollection

Image

ImageBrush

InkPresenter

Inlines

KeySpline

KeyTime

Line

LinearColorKeyFrame

LinearDoubleKeyFrame

LinearGradientBrush

LinearPointKeyFrame

LineBreak

LineGeometry

LineSegment

Matrix

MatrixTransform

MediaAttribute

MediaAttributeCollection

MediaElement

MultiScaleImage (Silverlight 2)

ObjectAnimationUsingKeyFrames (Silverlight 2)

ObjectKeyFrameCollection (Silverlight 2)

PasswordBox (Silverlight 2)

Path

PathFigure

PathFigureCollection

PathGeometry

PathSegment (abstract)

PathSegmentCollection

Point

PointAnimation

PointAnimationUsingKeyFrames

PointKeyFrame (abstract)

PointKeyFrameCollection

PolyBezierSegment

Polygon

Polyline

PolyLineSegment

PolyQuadraticBezierSegment

Popup (Silverlight 2)

QuadraticBezierSegment

RadialGradientBrush

Rect

Rectangle

RectangleGeometry

RepeatBehavior

ResourceDictionary

RotateTransform

Run

RuntimeErrorEventArgs

ScaleTransform

SkewTransform

SolidColorBrush

SplineColorKeyFrame

SplineDoubleKeyFrame

SplinePointKeyFrame

StackPanel (Silverlight 2)

Storyboard

Stroke

StrokeCollection

StylusInfo

StylusPoint

StylusPointCollection

TextBlock

TextBox (Silverlight 2)

Timeline (abstract)

TimelineCollection

TimelineMarker

TimelineMarkerCollection

TimelineMarkerEventArgs

TimeSpan

TransformCollection

TransformGroup

TranslateTransform

TriggerActionCollection

TriggerCollection

VisualCollection

VideoBrush

See Also

Reference