Hallo,
I have a listbox wich i populate with several items. I group these items by placing them in an exapnder. Problem is that when i refresh the listbox all the expanders get closed. Im trying to find a way to preserve the expanders state. Who can help.
This is what i have until now. Below is the important part: the listbox. One thing i have ben trying with: the EspandersState class. And the last: the collection of items for the listbox.
<UserControl.Resources><UserControl.Resources> more resource here</UserControl.Resources><ListBox ItemsSource="{Binding}" ItemTemplateSelector="{StaticResource templateSelector}" Name="myListBox" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" BorderBrush="Transparent"><ListBox.GroupStyle><GroupStyle><GroupStyle.ContainerStyle><Style TargetType="{x:Type GroupItem}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type GroupItem}"><Expander IsExpanded="{Binding Source={StaticResource myExpMgr}, Path=[Items[0].ObjectType]}"><Expander.Header><StackPanel Orientation="Horizontal" ><TextBlock Text="{Binding Path=Name}" /></StackPanel></Expander.Header><ItemsPresenter /></Expander></ControlTemplate></Setter.Value></Setter></Style></GroupStyle.ContainerStyle></GroupStyle></ListBox.GroupStyle></ListBox>
class ExpandersState { public Dictionary<object, bool> expandStates = new Dictionary<object, bool>(); public bool this[object key] { get { if (!expandStates.ContainsKey(key)) return false; return expandStates[key]; } set { if (!expandStates.ContainsKey(key)) expandStates.Add(this,value); expandStates[key] = value; } } } public ICollectionView GroupedProperties; private void setSelectedObject(object selectedDevice) { List<DependencyPropertyInfo> _listProperties = TypeHelper.GetObjectProperties(selectedDevice); GroupedProperties = new ListCollectionView(_listProperties); GroupedProperties.GroupDescriptions.Add(new PropertyGroupDescription("GroupType")); this.DataContext = GroupedProperties; }
Jc