如何在 WPF 数据网格中一起选择所有复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25043010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:15:02  来源:igfitidea点击:

How to Select All check box together in WPF datagrid

c#wpfwpfdatagrid

提问by user3847840

my wpf data grid is,

我的 wpf 数据网格是,

<dg:DataGrid.Columns >
    <dg:DataGridTemplateColumn>
        <dg:DataGridTemplateColumn.Header>
            <CheckBox Content=" Slect All" Click="CheckBox_Click" />
        </dg:DataGridTemplateColumn.Header>
        <dg:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox Name="chkSelectAll"   Margin="45 2 0 0" Click="chkSelectAll_Click" />
            </DataTemplate>
        </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>
    <dg:DataGridTemplateColumn Header="Edit Row"  >
        <dg:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Edit" Click="Button_Click" />
            </DataTemplate>
        </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>
    <dg:DataGridTemplateColumn Header="Delete Row">
        <dg:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Delete" Click="DeleteButton_Click" />
            </DataTemplate>
        </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>
</dg:DataGrid.Columns>

on click of this Select all checkbox I need to get all checkbox as selected/Unselect. Can any one help me to get it done. Also one more thing , Dynamicaly I am populating the data to the datgrid.

单击此 Select all 复选框时,我需要将所有复选框设为选中/取消选中。任何人都可以帮助我完成它。还有一件事,Dynamicaly 我正在将数据填充到 datgrid。

-- thanks

- 谢谢

回答by Rohit Vats

Bind the checkBox IsCheckedproperty to header checkBox using ElementName:

IsChecked使用ElementName以下命令将 checkBox属性绑定到 header checkBox :

<dg:DataGridTemplateColumn>
    <dg:DataGridTemplateColumn.Header>
        <CheckBox Content=" Slect All" x:Name="headerCheckBox" />
    </dg:DataGridTemplateColumn.Header>
    <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Name="chkSelectAll" Margin="45 2 0 0"
                      IsChecked="{Binding IsChecked, ElementName=headerCheckBox, 
                                          Mode=OneWay}" />
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>