其他分享
首页 > 其他分享> > CodeGo.net>如何访问ListBoxItems的可见性属性

CodeGo.net>如何访问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>

如何访问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;

标签:listboxitem,wpf,xaml,c
来源: https://codeday.me/bug/20191119/2036469.html