I use C # and WPF, and I have a ListView that contains items with a CheckBox in the first column. The ListView ItemsSource element is installed in the code (and not through the binding) and contains instances of the "Item" class with the "Name", "Type" and "Selected" properties.
public class Item : INotifyPropertyChanged { private string _name; private bool _selected; private string _type; public string Name { get { return _name; } set { _name = value; this.OnPropertyChanged(); } } public bool Selected { get { return _selected; } set { _selected = value; this.OnPropertyChanged(); } } public string Type { get { return _type; } set { _type = value; this.OnPropertyChanged(); } } protected virtual void OnPropertyChanged([CallerMemberName] string property = "") { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(property)); } public event PropertyChangedEventHandler PropertyChanged; }
The ListView View displays a GridView with the first column that is bound to the Selected property - for example, a flag means "selected."
I am adding grouping to this ListView (grouped by type), and GroupStyle also contains a CheckBox.
var lst = new List<Item>(); lst.Add(new Item { Name = "A", Type = "1" }); lst.Add(new Item { Name = "B", Type = "1" }); lst.Add(new Item { Name = "C", Type = "1" }); lst.Add(new Item { Name = "A", Type = "2" }); lst.Add(new Item { Name = "B", Type = "2" }); lst.Add(new Item { Name = "C", Type = "2" }); listview.ItemsSource = lst; var view = CollectionViewSource.GetDefaultView(lst); view.GroupDescriptions.Add(new PropertyGroupDescription("Type"));
XAML for ListView contains GridView and GroupStyle:
<ListView x:Name="listview"> <ListView.View> <GridView> <GridViewColumn Width="50"> <GridViewColumn.CellTemplate> <DataTemplate DataType="cls:Item"> <CheckBox IsChecked="{Binding Selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Width="300" Header="Name" DisplayMemberBinding="{Binding Name, UpdateSourceTrigger=PropertyChanged}"></GridViewColumn> <GridViewColumn Width="100" Header="Type" DisplayMemberBinding="{Binding Type}"></GridViewColumn> </GridView> </ListView.View> <ListView.GroupStyle> <GroupStyle> <GroupStyle.ContainerStyle> <Style TargetType="{x:Type GroupItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Expander IsExpanded="True"> <Expander.Header> <StackPanel Orientation="Horizontal"> <CheckBox></CheckBox> <TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding ItemCount, StringFormat='- {0} item(s)'}" /> </StackPanel> </Expander.Header> <ItemsPresenter /> </Expander> </ControlTemplate> </Setter.Value> </Setter> </Style> </GroupStyle.ContainerStyle> </GroupStyle> </ListView.GroupStyle> </ListView>
Finally, to my question: what I would like to do is use the CheckBox in the group header to select all or none of the elements in this particular group. For instance:

When you click on the group header checkbox, you should select all the elements in this particular group, if they are not already selected. When you press it again, you must remove (remove) all the elements in this group. If the user manually selects or deselects some elements in the group, it would be nice if the group header flag displayed an undefined state, but simply unchecked would be good.
I donโt know where to start. I assume that I need to associate the IsChecked property with the group header, but I do not know what to associate it with, since the datacontext will be something like GroupDescriptor that does not contain any information about the group, and what elements are in this group (right? )
I do not strictly follow MVVM, so I donโt worry about doing all this in bindings and in my viewmodel, I would be fine with something like listening to the marked flag event and somehow figure out in the code which elements must check. For instance; if I could listen to the Checked event and somehow extract the type of group in which I would be mostly installed (I could go through the entire list and select all of them with the corresponding group). But I donโt even see a way to do this; I can get the CheckBox in the Checked event (sender), and I can loop through all the parent controls, but nowhere I see a way to retrieve information about the property in which I group ...
Any help would be great!