How to access Visibility property of the ListBoxItems
Asked Answered
L

1

3

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;
Larrikin answered 5/11, 2015 at 19:22 Comment(0)
S
3

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;
Stephanystephen answered 5/11, 2015 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.