How to access Visibility property of the ListBoxItems

霸气de小男生 提交于 2019-12-18 06:57:57

问题


In my WPF program I have a ListBox control:

<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>

How can I access Visibility property of the ListBoxItems? My code doesn't works:

mailsListBox.Items[1].Visibility = Visible;

回答1:


Like this:

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

Or you can use ItemContainerGenerator.ContainerFromItem method:

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


来源:https://stackoverflow.com/questions/33553035/how-to-access-visibility-property-of-the-listboxitems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!