FrameworkTemplate.FindName Method

Finds the element associated with the specified name defined within this template.

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

public Object FindName(
	string name,
	FrameworkElement templatedParent


Type: System.String

The string name.

Type: System.Windows.FrameworkElement

The context of the FrameworkElement where this template is applied.

Return Value

Type: System.Object
The element associated with the specified name.

For more information, see "XAML Namescopes in Styles and Templates" in WPF XAML Namescopes.

This example shows how to find elements that are generated by a DataTemplate.

In this example, there is a ListBox that is bound to some XML data:

<ListBox Name="myListBox" ItemTemplate="{StaticResource myDataTemplate}"
    <Binding Source="{StaticResource InventoryData}" XPath="Books/Book"/>

The ListBox uses the following DataTemplate:

<DataTemplate x:Key="myDataTemplate">
  <TextBlock Name="textBlock" FontSize="14" Foreground="Blue">
      <Binding XPath="Title"/>

If you want to retrieve the TextBlock element generated by the DataTemplate of a certain ListBoxItem, you need to get the ListBoxItem, find the ContentPresenter within that ListBoxItem, and then call FindName on the DataTemplate that is set on that ContentPresenter. The following example shows how to perform those steps. For demonstration purposes, this example creates a message box that shows the text content of the DataTemplate-generated text block.

// Getting the currently selected ListBoxItem 
// Note that the ListBox must have 
// IsSynchronizedWithCurrentItem set to True for this to work
ListBoxItem myListBoxItem =

// Getting the ContentPresenter of myListBoxItem
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);

// Finding textBlock from the DataTemplate that is set on that ContentPresenter
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);

// Do something to the DataTemplate-generated TextBlock
MessageBox.Show("The text of the TextBlock of the selected list item: "
    + myTextBlock.Text);

The following is the implementation of FindVisualChild, which uses the VisualTreeHelper methods:

private childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
        if (child != null && child is childItem)
            return (childItem)child;
            childItem childOfChild = FindVisualChild<childItem>(child);
            if (childOfChild != null)
                return childOfChild;
    return null;

More Code

How to: Find ControlTemplate-Generated ElementsThis example shows how to find elements that are generated by a ControlTemplate.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft