【问题标题】:Datagrid Column header should check / uncheck CheckBox’s state depending upon whether all CheckBoxes of a DataGridView column are checked or uncheckedDatagrid 列标题应根据 DataGridView 列的所有 CheckBox 是选中还是未选中来检查/取消选中 CheckBox 的状态
【发布时间】:2012-04-29 18:47:08
【问题描述】:

我遇到的问题与 DataGrid(WPF) 中的复选框有关。为了更好地理解问题,我附上了屏幕截图。

问题:即使其中一个子项未选中,也会选中 DataHeader 列复选框。我希望解决方案能够解决此问题,以便当用户明确取消选中其中一个孩子时,应该隐式取消选中 ALL(Column Header)。

请帮助大家...谢谢 请检查链接。我希望解决方案像这样工作。 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; }
}

【问题讨论】:

    标签: wpf datagrid checkbox wpfdatagrid observablecollection


    【解决方案1】:
    //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;
        }
    }
    

    【讨论】:

    • IsChecked 是一个可为空的布尔值,因此您必须使用 ckbox.IsChecked == true 显式检查其值或使用 ckbox.IsChecked ?? false 合并它
    【解决方案2】:

    在您的 XAML 数据网格中添加:

    <DataGridTemplateColumn.Header>
    <CheckBox x:Name="ckbHeader" Click="ckbHeader_Click"></CheckBox>
    </DataGridTemplateColumn.Header>
    

    在您的代码中添加:

    var ckbox = sender as CheckBox;
    var All = Collection.View.SourceCollection as List<ObjectX>;
    
    if (ckbox.IsChecked)
    {
        foreach (var item in All)       
            item.Marked = true;     
    }
    else
    {
        foreach (var item in All)       
            item.Marked = false;        
    }
    Collection.View.Refresh();
    

    注意:发件人是 CheckBox

    【讨论】:

    • true 检查是否相等是多余的。就说if (ckbox.IsChecked)
    • NO,在 CheckBox 类型中,属性 IsChecked 可能为 null,那么,必须指定 true、false 或 null。尝试现实。
    猜你喜欢
    • 2019-02-14
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 2014-12-26
    • 2015-06-18
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    相关资源
    最近更新 更多