如何在列表视图的组标题中保存 IsExpanded 状态 [英] How to save the IsExpanded state in group headers of a listview

查看:26
本文介绍了如何在列表视图的组标题中保存 IsExpanded 状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很棘手的问题:

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>

如您所见,Expander 的 IsExpanded 属性设置为 true.这意味着每当 ListView 刷新时,所有 Expander 控件都会展开.

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.

然而,我确实想保存每个扩展器的最后状态.我一直无法找到一种方法来保存每个 ObjectType 的 Expander 状态列表.我正在尝试绑定 HashTable 和转换器,但未能将 ObjectType 作为 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? :)

推荐答案

你可以创建一个带有 Dictionary 的新类(比如,ObjectType 作为 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 在需要时调用代码并传递参数的好方法.(WPF 允许您将子表达式放在绑定路径中的索引器中,这对我来说是个新闻 - 虽然很好,不是吗!)

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!)

这篇关于如何在列表视图的组标题中保存 IsExpanded 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆