How to: Find DataTemplate-Generated Elements
TOC
Collapse the table of content
Expand the table of content

How to: Find DataTemplate-Generated Elements

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.


			' Getting the currently selected ListBoxItem
			' Note that the ListBox must have
			' IsSynchronizedWithCurrentItem set to True for this to work
			Dim myListBoxItem As ListBoxItem = CType(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem), ListBoxItem)

			' Getting the ContentPresenter of myListBoxItem
			Dim myContentPresenter As ContentPresenter = FindVisualChild(Of ContentPresenter)(myListBoxItem)

			' Finding textBlock from the DataTemplate that is set on that ContentPresenter
			Dim myDataTemplate As DataTemplate = myContentPresenter.ContentTemplate
			Dim myTextBlock As TextBlock = CType(myDataTemplate.FindName("textBlock", myContentPresenter), TextBlock)

			' 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 Function FindVisualChild(Of childItem As DependencyObject)(ByVal obj As DependencyObject) As childItem
			For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(obj) - 1
				Dim child As DependencyObject = VisualTreeHelper.GetChild(obj, i)
				If child IsNot Nothing AndAlso TypeOf child Is childItem Then
					Return CType(child, childItem)
				Else
					Dim childOfChild As childItem = FindVisualChild(Of childItem)(child)
					If childOfChild IsNot Nothing Then
						Return childOfChild
					End If
				End If
			Next i
			Return Nothing
		End Function


Show:
© 2016 Microsoft