私はListBoxの選択された要素のみを表示しています(ええ、私は知っています)...そして、ListBoxItemの単一の子を持つ単一のListBox.ItemTemplateがある場合、要素にアクセスするには2つのListBoxItemsをトラバースする必要がある理由を理解しようとしています「thisListBoxItem」という名前ですか?視覚的な ListBoxItem 要素は 1 つしかないようです。
私の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) にキャストします // 次に、FindName プロパティを介して下位の ListBoxItem に降りる必要があります。
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;
}