Reader Level:

Find Controls by Name in WPF

By Mahesh Chand on Oct 03, 2009
FindName method of FrameworkElement class is used to find elements or controls by their Name properties. This article shows how to find controls on a Window by name.

FindName method of FrameworkElement class is used to find elements or controls by their Name properties. The FrameworkElement class is mother of all controls in WPF.

Here is a Window with a Button, a TextBox, and a ListBox control.

<Window x:Class="FindControlByNameSample.Window1"



        Title="Window1" Height="305" Width="508">

    <Grid Name="LayoutRoot">

        <Button Content="Find By Name" Height="35" HorizontalAlignment="Left" Margin="18,23,0,0"

                Name="FindNameButton" VerticalAlignment="Top" Width="144" Click="FindNameButton_Click" />

        <TextBox Height="39" HorizontalAlignment="Left" Margin="202,21,0,0" Name="TextBox1"

                 VerticalAlignment="Top" Width="184" />

        <ListBox Height="146" HorizontalAlignment="Left" Margin="18,98,0,0" Name="listBox1"

                 VerticalAlignment="Top" Width="179" />




The following code snippet is a button click event handler and calls FindName method of Grid to find the control by its name. The code takes control name from the TextBox and checks of the found control is a TextBox, ListBox, or Button and based on its type, code sets its properties.

private void FindNameButton_Click(object sender, RoutedEventArgs e)


    object item = LayoutRoot.FindName(TextBox1.Text);

    if (item is TextBox)


        TextBox txt = (TextBox)item;

        txt.Text = "Aha! you found me!";

        txt.Background = new SolidColorBrush(Colors.LightYellow);


    else if (item is ListBox)


        ListBox lst = (ListBox)item;

        lst.Items.Add("Aha! you found me!");


    else if (item is Button)


        Button btn = (Button)item;

        btn.Content = "Aha! you found me!";

        btn.Background = new SolidColorBrush(Colors.LightSeaGreen);



If you enter "TextBox1" in the TextBox and click Find By Name button, you will see output like Figure 1.


Figure 1

Mahesh Chand
Mahesh Chand

C# Corner founder, 10-times Microsoft MVP, technical author, and software architect. Mahesh authored his first book, A Programmer’s Guide to ADO.NET in C# with APress at the age of 25. C# Corner is a memb... Read more



Spire.Doc - Free .NET Word API
Use Spire.Doc to create, read, write, print, and convert word documents to OpenXML, RTF, TXT, XPS, EPUB, EMF, HTML, Image and PDF.
Infragistics jQuery Controls

Trending up