1 out of 1 rated this helpful - Rate this topic

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
)

Parameters

name
Type: System::String

The string name.

templatedParent
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}"
         IsSynchronizedWithCurrentItem="True">
  <ListBox.ItemsSource>
    <Binding Source="{StaticResource InventoryData}" XPath="Books/Book"/>
  </ListBox.ItemsSource>
</ListBox>

The ListBox uses the following DataTemplate:

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

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.

No code example is currently available or this language may not be supported.

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

No code example is currently available or this language may not be supported.

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, 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.

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.