C#code在WPF数据网格中选择所有复选框网格、复选框、数据、code

2023-09-07 02:22:12 作者:跟哥混社会流血又流泪

我需要一些C#code选择/取消选择所有复选框在WPF 3.5框架数据网格。 我想通过单击电网单标题复选框做到这一点。

I need some c# code to select / deselect all checkboxes in a datagrid in WPF 3.5 framework. I would like to do this by clicking a single header checkbox in the grid.

请帮忙。

推荐答案

这可以声明进行。下面创建的每一行,并可以切换行选择的一个复选框列。复选框列的标题可点击做一个选择的所有行。

This can be done declaratively. The following creates a checkbox column for each row and which can toggle row selections. The header of the checkbox column can be clicked to do a select all of the rows.

从XAML的相关部分

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit">
    <toolkit:DataGrid Name="dataGrid" 
     ItemsSource="{Binding}" AutoGenerateColumns="True" 
     SelectionMode="Extended" CanResizeRows="False">
    <toolkit:DataGrid.RowHeaderTemplate>
       <DataTemplate>
           <Grid>
               <CheckBox IsChecked="{
                  Binding Path=IsSelected, 
                  Mode=TwoWay, 
                  RelativeSource={RelativeSource FindAncestor, 
                  AncestorType={x:Type toolkit:DataGridRow}}}"
            />
           </Grid>
       </DataTemplate>
    </toolkit:DataGrid.RowHeaderTemplate>
    </toolkit:DataGrid>
</Window>