在WPF datagrid标头中添加一个复选框,并使用它来选择/取消选择DataGridCheckBoxColumn中的所有复选框 [英] Add a checkbox in WPF datagrid header and use it to select/unselect all checkboxes in DataGridCheckBoxColumn

查看:553
本文介绍了在WPF datagrid标头中添加一个复选框,并使用它来选择/取消选择DataGridCheckBoxColumn中的所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的新手,希望能解决以下问题。

I am new to WPF and would appreciate help on following problem.

在wpf datagrid中,我将DataGridCheckBoxColumn作为第一列,并将此列绑定到IsSelected属性

In my wpf datagrid I have DataGridCheckBoxColumn as first column and I have bind this column to IsSelected property in ViewModel.

<toolkit:DataGridCheckBoxColumn Header="Title" Binding="{Binding isSelected}"/>

我还希望在标题行中有一个复选框,并打算用来选择/取消选择所有

I also want to have a checkbox in header row and which I intend to use to select/unselect all the checkboxes in this column.

到目前为止,我已经成功通过应用标题样式(如下面的代码片段所示)在标题中获得了一个复选框,但是我无法切换选择对于列中的所有复选框

Till now I have managed to get a checkbox in header by applying a headerstyle as shown in below code snip but I am not able to toggle selection for all the checkboxes in column

<Style x:Key="CheckBoxHeaderStyle" TargetType="{x:Type toolkit:DataGridColumnHeader}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type toolkit:DataGridColumnHeader}">
                <CheckBox x:Name="chkToggleSelection"   VerticalAlignment="Center">
                </CheckBox>
            </ControlTemplate>
        </Setter.Value>
     </Setter>
 </Style>


推荐答案

您将必须处理 Checkbox.Click 事件(如果您使用的是MVVM,请设置 CheckBox.Command 或使用附加行为来处理该事件),然后设置

You will have to handle the Checkbox.Click event (if you are using MVVM then set the CheckBox.Command or use attached behavior to handle the event) and then set the boolean property of all items bound to the datagrid as true \ false accordingly.

不幸的是,我没有其他选择!

Sadly there is no other alternative that I am aware of!

这篇关于在WPF datagrid标头中添加一个复选框,并使用它来选择/取消选择DataGridCheckBoxColumn中的所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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