FindName Method (DependencyObject)

Gets any object in the Silverlight object hierarchy by referencing the object's x:Name attribute value.

XAML
Cannot use methods in XAML.
Scripting
retval = object.FindName(name)

Parameters

name

string

The name of the object to get.

Return Value

object

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

Remarks

You can find any 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, a reference to the object is returned; otherwise, null is returned.

Some of the object types listed in Applies To are abstract types, therefore this method cannot be truly called on that type in JavaScript because there is no way to obtain instances of abstract types in JavaScript programming.

Examples

The following JavaScript example shows how a leaf node of a Silverlight object hierarchy can access a root node.

JavaScript
// Loaded event handler for the root Canvas object.
function onLoaded(sender, eventArgs)
{
    // Retrieve the object corresponding 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 SetValue operations as a combined operation:

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

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

Referencing and Modifying Objects
Name

Community Additions

Show: