如何保存在ListView的组头的IsExpanded状态 [英] How to save the IsExpanded state in group headers of a listview
问题描述
我有一个相当棘手的问题:
I have quite a tricky problem:
我使用的是ListView控件的的ItemsSource设置为CollectionViewSource包括PropertyGroupDescription分组ListView的元素。该CollectionViewSource看起来是这样的:
I am using a ListView control with the ItemsSource set to a CollectionViewSource including a PropertyGroupDescription to group the ListView elements. The CollectionViewSource looks like this:
<CollectionViewSource x:Key="ListViewObjects">
<CollectionViewSource.Source>
<Binding Path="CurrentListViewData"/>
</CollectionViewSource.Source>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="ObjectType" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
在我使用自定义组标题像这样的ListView:
In the ListView I use customize the group headers like this:
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<DockPanel>
<TextBlock Text="{Binding Path=Items[0].ObjectType />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
正如你可以看到扩展的IsExpanded属性设置为true。这意味着只要在ListView被刷新,所有的扩展控件扩展。
As you can see the IsExpanded property of the Expander is set to true. This means that whenever the ListView is refreshed, all Expander controls are expanded.
我也不过想节省每扩展的最后状态。我一直没能想出一个办法,以节省每对象类型扩展状态的列表。我用绑定的哈希表和转换实验,但我在提供对象类型为ConverterParameter失败了,因为它总是作为字符串传递。但是这可能不是溶液反正
I do however want to save the last state of every Expander. I haven't been able to figure out a way to save a list of Expander states per ObjectType. I was experimenting with a bound HashTable and a Converter, but I failed at providing the ObjectType as a ConverterParameter, because it was always passed as a string. But that may not be the solution anyways.
有人可以给我一个解决方案的提示或想法,请? :)
Can somebody give me a hint or an idea for a solution, please? :)
推荐答案
您可以创建一个新的类字典(例如,对象类型列为重点bool的值),并给它一个索引:
You could create a new class with a Dictionary (say, ObjectType as a key to bool values), and give it an indexer:
Dictionary<ObjectType, bool> expandStates = new Dictionary<ObjectType, bool>();
public bool this[ObjectType key]
{
get
{
if (!expandStates.ContainsKey(key)) return false;
return expandStates[key];
}
set
{
expandStates[key] = value;
}
}
然后,它实例在ResourceDictionary中的地方和IsExpanded绑定到它是这样的:
Then, instantiate it in a ResourceDictionary somewhere and bind the IsExpanded to it like this:
<Expander IsExpanded="{Binding Source={StaticResource myExpMgr}, Path=[Items[0].ObjectType]}">
这很可能做到这一点:让WPF打电话给你的code,只是传递参数当你需要它的一个很好的方式。 (那WPF可以让你把SUBEX pressions在索引中具有约束力的路径是新闻对我 - 好的,但不是吧!)
That might well do it: a nice way of getting WPF to call your code and pass a parameter just when you need it. (That WPF lets you put subexpressions in indexers in a binding path was news to me - good though isn't it!)
这篇关于如何保存在ListView的组头的IsExpanded状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!