Datagrid列标题应根据是否选中或取消选中DataGridView列的所有复选框来检查/取消选中CheckBox的状态 [英] Datagrid Column header should check / uncheck CheckBox’s state depending upon whether all CheckBoxes of a DataGridView column are checked or unchecked

查看:231
本文介绍了Datagrid列标题应根据是否选中或取消选中DataGridView列的所有复选框来检查/取消选中CheckBox的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我困扰的问题与DataGrid(WPF)中的复选框相关。我附上了截图,更好地了解这个问题。

The problem i'm stuck with is related to checkbox in DataGrid(WPF). I've attached the screenshot for better understanding of the problem.

问题:即使子项中的一个未选中,DataHeader列复选框也会被选中。我希望解决方案来解决这个问题,以便当一个孩子被用户明确地取消选中时,ALL(列标题)应该隐式取消选中。

Problem: The DataHeader Column Checkbox is checked even when one of the child is Unchecked. I expect the solution to fix this so that when one of the child is unchecked explicitly by the user, The ALL(Column Header) should be unchecked implicitly.

请帮助家伙...谢谢
Plz检查链接。我想要解决方案这样工作。 http:// www .codeproject.com / Articles / 42437 / Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat#

Please help guys... Thank You Plz check the link. i want the solution to work like this. http://www.codeproject.com/Articles/42437/Toggling-the-States-of-all-CheckBoxes-Inside-a-Dat#

<dg:DataGrid.Columns>
    <dg:DataGridCheckBoxColumn Binding="{Binding Check}" IsThreeState="True" Width="50">
        <dg:DataGridCheckBoxColumn.HeaderTemplate>
            <DataTemplate x:Name="dtAllChkBx">
                <CheckBox Name="cbxAll" Content="{x:Static properties:Resources.lblAll}"
                          Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
            </DataTemplate>
        </dg:DataGridCheckBoxColumn.HeaderTemplate>
    </dg:DataGridCheckBoxColumn>

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    unchck_all_prd();
    dgEnggAcc.Items.Refresh();
}

private void unchck_all_prd()
{
    for (int i = 0; i < engg_list.Count; i++)
    {
        engg_list[i].Check = false;
    }
}

private void chck_all_prd()
{
    for (int i = 0; i < engg_list.Count; i++)
    {
        engg_list[i].Check = true;
    }
}

public class EnggLst : ObservableCollection<EnggLst>
{
    public bool Check { get; set; }
}


推荐答案

//this event is for **Checked and UnChecked** of up check box (cbxall)
private void UpCheckbox_Checked(object sender, RoutedEventArgs e)
{
    //checkBox1 = cbxall (your up checkbox)
    if (checkBox1.IsChecked == true)
    {
        dataGrid1.Items.OfType<YourClass>().ToList().ForEach(x => x.IsChecked = true);
    }
    else
    {
        dataGrid1.Items.OfType<YourClass>().ToList().ForEach(x => x.IsChecked = false);
    }
}

//this event is for all other check box
//**Checked and UnChecked** of all other check box is this event
private void OtherCheckbox_Checked(object sender, RoutedEventArgs e)
{
    //checkBox1 = cbxall (your up checkbox)
    if (dataGrid1.Items.OfType<YourClass>().All(x => x.IsChecked == true))
    {
        checkBox1.IsChecked = true;
    }
    else if (dataGrid1.Items.OfType<YourClass>().All(x => x.IsChecked == false))
    {
        checkBox1.IsChecked = false;
    }
    else
    {
        checkBox1.IsChecked = null;
    }
}

希望这有助于你的好运。

hope this help you Good Luck.

这篇关于Datagrid列标题应根据是否选中或取消选中DataGridView列的所有复选框来检查/取消选中CheckBox的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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