简体   繁体   中英

How do I write code to select/unselect all checkbox on selecting checkbox header of data grid WPF?

I am facing little issues on working with data grid in WPF. Previously I was comfortable with Windows forms.

I have added a checkbox column in data grid and added a checkbox in the header.

Now How do I write code to select/unselect all checkbox on selecting checkbox header?

I tried all the possible answers from other posts but I am unable to success on it. I am little confused somewhere.

<DataGrid AutoGenerateColumns="True" Height="204" HorizontalAlignment="Left" Margin="38,162,0,48" Name="dataGrid1" VerticalAlignment="Stretch" Width="729" AreRowDetailsFrozen="False"  EnableColumnVirtualization="False" IsManipulationEnabled="False" CanUserAddRows="False" AutoGeneratingColumn="dataGrid1_AutoGeneratingColumn" RowHeight="26" ColumnHeaderHeight="26" FontSize="15">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn >
            <DataGridCheckBoxColumn.Header>
                <CheckBox Name="SelectAll" ></CheckBox>
            </DataGridCheckBoxColumn.Header>
        </DataGridCheckBoxColumn>
    </DataGrid.Columns>
</DataGrid>

What should I do next?

In WPF, you're supposed to use data binding. So in your case, you should data bind a bool property to the CheckBox in your Header and another for each item in the collection:

<DataGridCheckBoxColumn Binding="{Binding IsSelected, Mode=TwoWay}">
    <DataGridCheckBoxColumn.Header>
        <CheckBox Name="SelectAll" IsChecked="{Binding AreAllCheckBoxesChecked, 
            RelativeSource={RelativeSource AncestorType={x:Type YourPrefix:YourWindow}}}" />
</DataGridCheckBoxColumn>

In this example, the items in your collection will need to have an IsSelected property to data bind to the DataGridCheckBoxColumn and your view model or code behind will need an AreAllCheckBoxesChecked property to data bind to the Header Checkbox.IsChecked property. Then, it's a simple matter of updating the IsSelected property of each collection item in the setter of the AreAllCheckBoxesChecked property:

public bool AreAllCheckBoxesChecked
{
    get { return areAllCheckBoxesChecked; }
    set 
    {
        areAllCheckBoxesChecked = value;
        foreach (YourDataType item in YourCollection)
        {
            item.IsSelected = value;
        }
        NotifyPropertyChanged("AreAllCheckBoxesChecked"); 
    }
}

You need to bind checkboxes to some properties of VM if you are following MVVM, if not, instead of IsSelected, you can have an event handler for checked and unchecked events.

<DataGridTemplateColumn>
                            <DataGridTemplateColumn.HeaderTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="{Binding SelectAll}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.HeaderTemplate>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="{Binding IsSelected}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM