Introduction
In my previous article we had seen how to achieve grouping and sorting in ListBox. In this article we will see how can we Expand and Collapse the Header and it’s Contents.
Creating WPF Project
Fire up Visual Studio 2008 and create a new WPF Project. Name it as ListBoxSampleWPF.
Now we will add a ListBox to the Application and it will look as following:
Now let’s add some sample data to the ListBox.
public {
} ObservableCollection<Person> myList;
{ InitializeComponent(); myList = new {
}; lbPersonList.ItemsSource = myList; } |
Now we would like to see the Persons from Different Countries. Then add the following code.
ICollectionView view = CollectionViewSource.GetDefaultView(myList); view.GroupDescriptions.Add(new view.SortDescriptions.Add(new
lbPersonList.ItemsSource = view; |
And in XAML we need to add the GroupStyle for the ListBox.
<ListBox x:Name=”lbPersonList” Margin=”19,17,162,25″ AlternationCount=”2″>
|
Now run the application and you will see the Sorting in the Grouped contents too.
Now we will add the code for Expand and Collapse of Header.
Now add a style to the Window.Resources as follows:
<Style x:Key=”ContainerStyle” TargetType=”{x:Type GroupItem}”>
|
Add the Style to the GroupStyle of ListBox.
<ListBox.GroupStyle>
|
Now run the application to See the Expand and Collapse of Grouped Headers.
That’s it. Hope this article helps.
Posted in List Box - WPF, WPF Tagged: Group Header Expand Collapse, Grouping, ListBox, WPF
