Я только отображаю выбранный элемент ListBox ( да, я знаю ) ... И я пытаюсь понять, почему, если у меня есть один ListBox.ItemTemplate с единственным дочерним элементом ListBoxItem, я должны пройти 2 ListBoxItems для доступа к элементу с именем "thisListBoxItem"? Кажется, должен быть только один визуальный элемент ListBoxItem.
мой XAML
<ListBox Name="cjisDisplayItemListBox" SelectionChanged="cjisDisplayItemListBox_SelectionChanged_1">
<ListBox.ItemTemplate >
<DataTemplate>
<ListBoxItem Name="thisListBoxItem" Visibility="Collapsed">
<!-- some TextBlocks with bindings here -->
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
// Сначала я приводил SelectedItem к ListBoxItem (myListBoxItem)
// тогда я должен опуститься до нижнего ListBoxItem через свойство FindName ..
private void cjisDisplayItemListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
ListBox lb = sender as ListBox;
object item = lb.SelectedItem as object;
ListBoxItem myListBoxItem = (ListBoxItem)(lb.ItemContainerGenerator.ContainerFromItem(item));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
if (myContentPresenter == null) return;
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
ListBoxItem temp = (ListBoxItem)myDataTemplate.FindName("thisListBoxItem", myContentPresenter);
if (myListBoxItem.IsSelected) temp.Visibility = System.Windows.Visibility.Visible;
}