Практическое руководство. Поиск элементов, созданных с использованием шаблона DataTemplate

В этом примере демонстрируется порядок поиска элементов, которые создаются с использованием шаблона DataTemplate.

Пример

В этом примере используется объект ListBox, который связан с определенными данными XML:

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

Для объекта ListBox используется следующий шаблон DataTemplate:

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

Чтобы получить элемент TextBlock, созданный с использованием шаблона DataTemplate конкретного объекта ListBoxItem, необходимо получить объект ListBoxItem, найти объект ContentPresenter в полученном объекте ListBoxItem, а затем вызвать метод FindName для шаблона DataTemplate, который установлен для этого объекта ContentPresenter. В следующем примере описывается порядок выполнения этих шагов. В целях демонстрации, в этом примере создается окно сообщения, в котором отображается текстовое содержимое блока, созданного с использованием шаблона DataTemplate.

            ' 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)
// Getting the currently selected ListBoxItem
// Note that the ListBox must have
// IsSynchronizedWithCurrentItem set to True for this to work
ListBoxItem myListBoxItem =
    (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));

// 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);

Ниже приведена реализация FindVisualChild, в которой используются методы VisualTreeHelper.

        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
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;
        else
        {
            childItem childOfChild = FindVisualChild<childItem>(child);
            if (childOfChild != null)
                return childOfChild;
        }
    }
    return null;
}

См. также

Задачи

Поиск элемента, созданного шаблоном ControlTemplate

Основные понятия

Общие сведения о связывании данных

Стилизация и использование шаблонов

Области видимости имен XAML в WPF

Деревья в WPF

Другие ресурсы

Практические руководства по привязке данных