问题描述
我绑定到一些数据一个ListView,它的分组和排序方式。我添加了一个复选框向分组头部像这样:
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?
推荐答案
不幸的是它不是那么简单,因为它需要,
Sadly its not as straightforward as it needs to be,
请记住,一个小组的头复选框
有一个的DataContext
这是一个特殊的对象名为 GroupItem
。在这种 GroupItem
有哪些是该集团再度presents上,即共同的价值值名称
属性该分组的基础已经发生。
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.
很多人混淆这与群描述属性如假设你已经添加了一个 GroupDescription
与财产的employeeStatus
你的的CollectionView
的员工,那么 GroupItem.Name
不是的employeeStatus
,但它实际上是在哪一组是创造了这样的价值为 present
,缺席
, OnLeave
等。
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 ...
-
我们命名头复选框,说HeaderCheckBox
We name the header checkbox, say "HeaderCheckBox"
<CheckBox x:Name="HeaderCheckBox" ...>
我们处理 Button.Click
在ListView的水平(冒泡附加事件)。
We handle Button.Click
(a bubbling attached event) at the ListView level.
<ListView Button.Click="HandleCheckBoxClick" ...>
在处理程序 HandleButtonClick
我们做以下code ....
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.
}
}
}
不幸的是,这是实现你寻求的唯一途径。如果你正在使用MVVM,那么整个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.
这篇关于在分组报头用的ListView复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!