次の方法で共有


FindName (DependencyObject)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a named object in the Silverlight object tree by referencing the object's Name or x:Name attribute value.

retval = object.FindName(name)

Arguments

name

string

The name of the object to get.

Return Value

Type: object

A reference to the specified object if the object was successfully retrieved; otherwise, null.

Managed Equivalent

FrameworkElement.FindName (or TextElement.FindName). XAML namescopes work a bit differently in the managed API; see XAML Namescopes.

Remarks

You can find any named object in the Silverlight object hierarchy by using the FindName method and referencing the object's x:Name or Name attribute value. This means that the object you are searching for does not have to be a direct descendant of the object that invokes the FindName method. If the method is successful, it returns a reference to the object; otherwise, it returns null.

Because FindName relies on all XAML being parsed for the Name attributes, you should not attempt to call FindName until the XAML is loaded. You can assure this by calling FindName as part of a Loaded handler, or you can use other user-driven event handlers that implicitly can occur only once the user interface (UI) is loaded.

Some of the object types listed in the Applies To section are abstract types. The FindName method cannot truly be called on those types in JavaScript because there is no way to obtain instances of abstract types in JavaScript programming.

The behavior of FindName potentially depends on how or whether discrete XAML namescopes exist in your markup. For more information, see XAML and XAML-Related Concepts in the JavaScript API for Silverlight.

Example

The following JavaScript example shows how to find an object as soon as the XAML is loaded.

// Create the Loaded event handler for the root Canvas object.
function onLoaded(sender, eventArgs)
{
    // Retrieve the object that corresponds to the x:Name attribute value.
    var found = sender.findName("RedRect");

    // Determine whether the object was found.
    if (found != null)
    {
        alert(found.toString());
    }
    else
    {
        alert("Object not found");
    }
}

You can set or get a property directly on the returned value of the FindName method. The following JavaScript example shows how to combine the FindName and property set operations as one operation.

// Set the property on the retrieved object.
    sender.findName("statusTextBlock").text = "Click OK to continue.";

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