Hi,
I am using a WPF Listbox which is databound to an ICollectionView to display a list of items that can be filtered. And I am using context menu on the ListBox to group the items in the Listbox. For this, I have defined a groupstyle on the Listbox and it has an expander in it. The code for the same is as below. All the xaml code is in the Generic.xaml file and the control which has this ListBox is a custom control defined in the CustomControls library.
<ListBox.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander Style="{StaticResource GroupBoxExpander}" IsExpanded="True"
BorderBrush="Black" BorderThickness="0,0,0,1" x:Name="expander">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}"
Margin="5,0,0,0" Width="100"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter/>
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
Using the ICollectionView's filter method I filter the items in the Listbox, based on the string that is entered in the text box which is assciated with the ListBox. So, even when the group is collapsed, if the item for which searching is done is inside the collapsed expander, the expander is expanded and the item is visible in the Listbox. But, the problem is when the search string is removed from the text box, the expander should get back to its previous state, like in this state to its collapsed state. For this, i am trying to get the exapnder's IsExpanded property and based on its value, setting the expander's state to its previous state after the searching is finished.
I am trying to get the expander in the following way, but the catch here is it can only happen when the listbox's selection changed event fires:
FrameworkElement item = listbox.ItemContainerGenerator.ContainerFromItem(listbox.SelectedItem) as FrameworkElement; if (item != null) { GroupItem groupItem = item.GetVisualParent(); if (groupItem != null) { Expander expander = groupItem.Template.FindName("expander", groupItem) as Expander; if (expander != null) { expander.Collapsed += new RoutedEventHandler(expander_Collapsed); } } }
Could anyone please let me know if this would be a right appraoch and how to get access of the expander's IsExpanded property in the code beside file or can i try to attempt it in a different way possible?
Sowmi.