如何通过单击DatagridCheckBox列的CheckBox列标题来选择所有复选框? [英] How to select all checkboxes by clicking on CheckBox column header of a DatagridCheckBox Column?

查看:45
本文介绍了如何通过单击DatagridCheckBox列的CheckBox列标题来选择所有复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一个复选框列的数据网格.如果任何人单击列标题中的复选框,则应选中该特定列中的所有复选框.如何使用XAML来实现?

I have a datagrid with one checkbox column. If anyone clicks on checkbox present in the column header, all check boxes present in that particular column should be checked. How can it be achieved using XAML?

Xaml:

<DataGrid AlternationCount="2" AutoGenerateColumns="False" ItemsSource="{Binding}"  Height="325" HorizontalAlignment="Left" Margin="0,178,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="733" >
            <DataGrid.Columns>                
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.Header>
                        <CheckBox Name="colCheckBox" Content="Select All" Width="70" Checked="colCheck_Checked" />
                    </DataGridTemplateColumn.Header>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>                            
                                <CheckBox Name="rowCheckBox"  HorizontalAlignment="Center"/>                           
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="ProjectId" Binding="{Binding ProjectId}" Width="200"/>
                <DataGridTextColumn Header="BaselineStartDate" Binding="{Binding BaselineStartDate}" Width="200"/>
                <DataGridTextColumn Header="BaselineEndDate" Binding="{Binding BaselineEndDate}" Width="200"/>
            </DataGrid.Columns>
        </DataGrid>

推荐答案

您能否仅将数据绑定到 Header 直接指向行中 CheckBox es的 IsChecked 属性?这应该可以解决问题:

Can you not just data bind the IsChecked property of the CheckBox in the Header straight to the IsChecked property of the CheckBoxes in the rows? This should do the trick:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.Header>
        <CheckBox Name="colCheckBox" Content="Select All" Width="70" />
    </DataGridTemplateColumn.Header>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Name="rowCheckBox" IsChecked="{Binding IsChecked, 
                ElementName=colCheckBox}" HorizontalAlignment="Center"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

当然,这还允许用户取消选中任何行中的所有 Checkbox es,而您可能不希望这样做.另外,如果项目类中具有 bool 属性,则可以使用数据绑定:

Of course, this would also allow users to uncheck all of the Checkboxes from any row and you might not want that. Alternatively, you could use data binding if you had a bool property in your item class:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.Header>
        <CheckBox IsChecked="{Binding MasterIsChecked}" Content="Select All" />
    </DataGridTemplateColumn.Header>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsChecked}" HorizontalAlignment="Center"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

然后通过视图模型(或后面的代码)中的 MasterIsChecked 属性更新项目:

And then update the items from the MasterIsChecked property in the view model (or code behind):

public bool MasterIsChecked
{
    get { return masterIsChecked; }
    set 
    {
        masterIsChecked = value; 
        NotifyPropertyChanged("MasterIsChecked");
        foreach (YourClass item in YourItems) item.IsChecked = masterIsChecked;
    }
}

这篇关于如何通过单击DatagridCheckBox列的CheckBox列标题来选择所有复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆