如何访问 ListBoxItems 的可见性 属性

How to access Visibility property of the ListBoxItems

在我的 WPF 程序中,我有一个 ListBox 控件:

<ListBox x:Name="mailsListBox" SelectionChanged="mailsListBox_SelectionChanged" >
    <ListBoxItem Content="..." Background="#FFF3F3F3" Margin="0,0,0,1" />
    <ListBoxItem Content="...." Margin="0,0,0,1" Visibility="Hidden"/>
</ListBox>

如何访问 ListBoxItemsVisibility 属性?我的代码不起作用:

mailsListBox.Items[1].Visibility = Visible;

像这样:

(mailsListBox.Items[1] as ListBoxItem).Visibility = Visibility.Visible;

或者你可以使用ItemContainerGenerator.ContainerFromItem方法:

((ListBoxItem)(mailsListBox.ItemContainerGenerator.ContainerFromItem(mailsListBox.Items[1]))).Visibility = Visibility.Visible;