10

在此处输入图像描述我遇到的问题与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; }
}
4

2 回答 2

3
//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;
    }
}
于 2012-05-07T04:41:46.633 回答
0

在您的 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

于 2017-07-22T23:46:03.550 回答