标题中带有复选框的分组 ListView
我有一个绑定到一些数据的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遗憾的是,它并不像需要的那么简单,
请记住,组的标头
CheckBox
有一个DataContext
,它是一种称为GroupItem
的特殊对象。 。在此GroupItem
中,有Name
属性,它是组表示的值,即发生分组的基础上的公共值。许多人将其与组描述属性混淆,例如假设您在员工的
CollectionView
中添加了带有属性EmployeeStatus
的GroupDescription
,那么GroupItem.Name
不是EmployeeStatus
,但它实际上是创建组的值,例如Present
、Absent
、OnLeave
等。有了这些知识,让我们尝试实现您所寻求的...
我们将标题复选框命名为“HeaderCheckBox”
我们在 ListView 级别处理
Button.Click
(冒泡附加事件)。在处理程序
HandleButtonClick
中,我们执行以下代码......遗憾的是,这是实现您所寻求的目标的唯一方法。如果您使用 MVVM,那么整个代码将必须通过附加行为来完成。
让我知道这是否有帮助。
Sadly its not as straightforward as it needs to be,
Remember that a group's header
CheckBox
has aDataContext
which is a special kind of object calledGroupItem
. In thisGroupItem
there isName
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 propertyEmployeeStatus
in yourCollectionView
of employees, thenGroupItem.Name
is NOTEmployeeStatus
but it is in fact the value on which group was created such asPresent
,Absent
,OnLeave
etc.Having this knowledge, lets try to achieve what you seek ...
We name the header checkbox, say "HeaderCheckBox"
We handle
Button.Click
(a bubbling attached event) at the ListView level.In the handler
HandleButtonClick
we do the following code....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.