微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

按扩展器分组的 DataGridRows - 如何定义多个 IsExpanded 状态?

如何解决按扩展器分组的 DataGridRows - 如何定义多个 IsExpanded 状态?

我在 WPF 应用程序中有一个数据网格,其中我定义了一个 GroupStyle,如下所示:

<DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style targettype="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate targettype="{x:Type GroupItem}">
                                        <StackPanel>                                                
                                            <Expander Header="{Binding Name}" 
                                                      FontWeight="Bold" 
                                                      FontSize="14" 
                                                       Background="#FF5CB9EE"> 
                                                <StackPanel>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="{Binding ItemCount,StringFormat=Student: {0}}" Margin="30,0" />
                                                    </StackPanel>
                                                    <ItemsPresenter/>
                                                </StackPanel>
                                            </Expander>                                                
                                        </StackPanel>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>

datagrid 绑定到一个 CollectionViewSource,它的源是一个 ObservableCollection。在我的数据网格中,我有以下样式和 DataGridColumnTemplate:

  <DataGrid.Resources>
                <Style x:Key="GroupHeaderStyle" targettype="DataGridColumnHeader">
                    <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},Path=DataContext.GroupSortCommand}"/>
                </Style>
            </DataGrid.Resources>

<DataGridTemplateColumn Header="SchoolClass" HeaderStyle="{StaticResource GroupHeaderStyle}">                        
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>                                
                            <TextBlock Text="{Binding StudentEducational.ClassLabel}" HorizontalAlignment="Center" Margin="5"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

样式适用于“SchoolClass”列:通过单击它的标题,我会在我的视图模型中调用 GroupSortCommand。 现在一切正常。

问题:我希望实现以下行为:

  • 如果您单击“SchoolClass”列的标题一次,则所有分组元素的扩展器都应折叠。
  • 如果您再次单击标题,则所有分组元素的扩展器都应展开。
  • 如果您第三次单击标题,则所有分组元素的扩展器都应折叠,除了所选元素(如果有)。在此状态下,您可以根据需要展开/折叠任何组。

我该如何解决这个问题?

请注意:我不会寻求实现逻辑本身的帮助(这是最简单的部分),而是关于如何设置所选组或所有组的 IsExpanded 属性。我知道这些组将位于某种列表(大概是 CollectionViewSource)中的某个位置,但是我如何利用它来发挥我的优势?

如果我在我的 viewmodel 中公开了一个 bool 类型的属性 IsGroupExpanded,我可以像这样将它应用到我的 XAML(这是来自 DataGrid ControlTemplate 的一个片段(见上面的代码)):

<ControlTemplate targettype="{x:Type GroupItem}">
                                        <StackPanel>                                               
                                            <Expander Header="{Binding Name}"  IsExpanded="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl},Path=DataContext.IsGroupExpanded}"
                                                      FontWeight="Bold" FontSize="14" Background="#FF5CB9EE" ExpandDirection="Down">
                                                <StackPanel>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="{Binding ItemCount,StringFormat=Schüler: {0}}" Margin="30,0" />
                                                    </StackPanel>
                                                    <ItemsPresenter/>
                                                </StackPanel>
                                            </Expander>                                                
                                        </StackPanel>
                                    </ControlTemplate>

我现在可以在我的 viewmodel 中设置“IsGroupExpanded”值(bool btw),以便所有组同时展开/折叠。

这样,我可以从我的列表中划掉我想要的三个行为中的两个。但是如何进入初始状态(每个组可以随意展开/折叠)?还是对待选中的组元素?

提前致谢 - 哦,大家新年快乐 ;-)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?