标题中带有复选框的分组 ListView

发布于 2024-12-03 02:51:52 字数 2242 浏览 3 评论 0原文

我有一个绑定到一些数据的 ListView,并且它已分组和排序。我在分组标题中添加了一个复选框,如下所示:

            <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,5"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="True" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1">
                                        <Expander.Header>
                                            <DockPanel>
                                                <CheckBox>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0"/>
                                                        <TextBlock Text=" ("/>
                                                        <TextBlock Text="{Binding Path=ItemCount}"/>
                                                        <TextBlock Text=" Items)"/>
                                                    </StackPanel>
                                                </CheckBox>
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter />
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>

现在我只关心并且需要一种方法来循环检查标题的分组项目,完成此操作的最佳方法是什么?

I have a ListView bound to some data, and it's grouped and sorted. I added a checkbox to to the grouping header like so:

            <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,5"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="True" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1">
                                        <Expander.Header>
                                            <DockPanel>
                                                <CheckBox>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0"/>
                                                        <TextBlock Text=" ("/>
                                                        <TextBlock Text="{Binding Path=ItemCount}"/>
                                                        <TextBlock Text=" Items)"/>
                                                    </StackPanel>
                                                </CheckBox>
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter />
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>

Now I only care and need a way to loop through the grouped items that have the header checked, what's the best way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟沫凡尘 2024-12-10 02:51:52

遗憾的是,它并不像需要的那么简单,

请记住,组的标头 CheckBox 有一个 DataContext,它是一种称为 GroupItem 的特殊对象。 。在此GroupItem 中,有Name 属性,它是组表示的值,即发生分组的基础上的公共值。

许多人将其与组描述属性混淆,例如假设您在员工的 CollectionView 中添加了带有属性 EmployeeStatusGroupDescription,那么 GroupItem.Name 不是 EmployeeStatus,但它实际上是创建组的值,例如 PresentAbsentOnLeave 等。

有了这些知识,让我们尝试实现您所寻求的...

  1. 我们将标题复选框命名为“HeaderCheckBox”

    
    
  2. 我们在 ListView 级别处理 Button.Click (冒泡附加事件)。

    
    
  3. 在处理程序HandleButtonClick中,我们执行以下代码......

    private void HandleCheckBoxClick(object sender, RoutedEventArgs e)
    {
        var checkBox = e.OriginalSource as CheckBox;
        if (checkBox != null && checkBox.Name == "HeaderCheckBox")
        {
            var groupItem = checkBox.DataContext as GroupItem;
    
            //// 假设 MyItem 是项目级别类并且 MyGroupedProperty 
            //// 是您已添加到分组中的分组属性
            //// CollectionView 中的描述。
            foreach(groupItem.Items 中的 MyItem 项目)
            {
                 //// 将项目代码放置在该特定组下。
            }
        }
    } 
    

遗憾的是,这是实现您所寻求的目标的唯一方法。如果您使用 MVVM,那么整个代码将必须通过附加行为来完成。

让我知道这是否有帮助。

Sadly its not as straightforward as it needs to be,

Remember that a group's header CheckBox has a DataContext which is a special kind of object called GroupItem. In this GroupItem there is Name property which is the value that the group represents i.e. the common value on the basis of which grouping has occurred.

Many people confuse this with the group description property e.g. assuming you have added a GroupDescription with property EmployeeStatus in your CollectionView of employees, then GroupItem.Name is NOT EmployeeStatus but it is in fact the value on which group was created such as Present, Absent, OnLeave etc.

Having this knowledge, lets try to achieve what you seek ...

  1. We name the header checkbox, say "HeaderCheckBox"

    <CheckBox x:Name="HeaderCheckBox" ...>
    
  2. We handle Button.Click (a bubbling attached event) at the ListView level.

    <ListView Button.Click="HandleCheckBoxClick" ...>
    
  3. In the handler HandleButtonClick we do the following code....

    private void HandleCheckBoxClick(object sender, RoutedEventArgs e)
    {
        var checkBox = e.OriginalSource as CheckBox;
        if (checkBox != null && checkBox.Name == "HeaderCheckBox")
        {
            var groupItem = checkBox.DataContext as GroupItem;
    
            //// Assuming MyItem is the item level class and MyGroupedProperty 
            //// is the grouped property that you have added to the grouped
            //// description in your CollectionView.
            foreach (MyItem item in groupItem.Items)
            {
                 //// Place your code for the items under that particular group.
            }
        }
    } 
    

Sadly this is the only way to achieve what you seek. If you are using MVVM, then the entire code will have to be done through an attached behavior.

Let me know if this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文