本文介绍了Datagrid Column 标题应检查/取消选中 CheckBox 的状态,具体取决于 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 Column Checkbox.我希望解决方案可以解决这个问题,以便当用户明确取消选中其中一个子项时,应该隐式取消选中 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.

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

Please help guys... Thank YouPlz 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;
    }
}

这篇关于Datagrid Column 标题应检查/取消选中 CheckBox 的状态,具体取决于 DataGridView 列的所有 CheckBox 是选中还是未选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 16:50