.NET Framework Class Library
FrameworkElement..::.FindName Method

Finds an element that has the provided identifier name.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
Syntax

Visual Basic (Declaration)
Public Function FindName ( _
    name As String _
) As Object
Visual Basic (Usage)
Dim instance As FrameworkElement
Dim name As String
Dim returnValue As Object

returnValue = instance.FindName(name)
C#
public Object FindName(
    string name
)
Visual C++
public:
Object^ FindName(
    String^ name
)
JScript
public function FindName(
    name : String
) : Object
XAML
You cannot use methods in XAML.

Parameters

name
Type: System..::.String
The name of the requested element.

Return Value

Type: System..::.Object
The requested element. This can be nullNothingnullptra null reference (Nothing in Visual Basic) if no matching element was found.
Remarks

If the element has child elements, these child elements are all searched recursively for the requested named element.

FindName operates within the current element's namescope. For details, see WPF XAML Namescopes.

Examples

This example describes how to use the FindName method to find an element by its Name value.

In this example, the method to find a particular element by its name is written as the event handler of a button. stackPanel is the Name of the root FrameworkElement being searched, and the example method then visually indicates the found element by casting it as TextBlock and changing one of the TextBlock visible UI properties.

C#
void Find(object sender, RoutedEventArgs e)
{
    object wantedNode = stackPanel.FindName("dog");
    if (wantedNode is TextBlock)
    {
        // Following executed if Text element was found.
        TextBlock wantedChild = wantedNode as TextBlock;
        wantedChild.Foreground = Brushes.Blue;
    }
}

For the complete sample, see Searching for an Element Sample.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Other Resources

Tags :


Page view tracker