在我的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>
如何访问ListBoxItems的Visibility属性?我的代码不起作用:
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;