-1

Here are my requirements:

  1. When the header check box is checked, all the checkboxes in that column will be checked. When the header check box is unchecked, all the checkboxes in that column will be unchecked.

  2. When all checkboxes in the column are cheched, uncheck one of them will uncheck the header checkbox. When check one by one checkboxes in the column, the header checkbox will be checked.

How do I achieve this?

Here is my xaml:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.Header>
        <CheckBox x:Name="chbHeader" IsChecked={"Binding AllChecked"} />
    </DataGridTemplateColumn.Header>

    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsChecked, ElementName=chbHeader}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
tweety
  • 31
  • 10
  • 1
    https://stackoverflow.com/questions/48955781/wpf-select-all-checkbox-in-a-datagrid/48989696 This will be the exact solution you need. – mohammed mazin Apr 29 '20 at 15:27
  • I got the ItemSource updated, but the UI did not work, ie the checkboxes do not get updated. – tweety Apr 29 '20 at 18:21

1 Answers1

0

I got it working.

<DataGridTemplateColumn>
    <DataGridTemplateColumn.Header>
        <CheckBox IsChecked="{Binding DataContext.AllSelected, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                  Command="{Binding DataContext.CmdSetChildCheckboxes, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
    </DataGridTemplateColumn.Header>

    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding DataContext.IsSelected, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                      Command="{Binding DataContext.CmdSetHeaderCheckbox, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

In C# code, have the properties IsChecked/AllChecked, and commands implemented accordingly.

tweety
  • 31
  • 10