我遇到的问题与DataGrid(WPF)中的复选框有关。我已附上截图以更好地理解问题。
问题:即使其中一个子项未选中,也会检查DataHeader列复选框。我希望解决方案能够解决这个问题,以便在用户明确取消选中其中一个子节点时,应该隐式取消选中ALL(列标题)。
请帮帮我们......谢谢 Plz检查链接。我希望解决方案能够像这样工作。 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; }
}
答案 0 :(得分:4)
//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;
}
}
答案 1 :(得分:1)
在您的XAML Datagrid中添加:
<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 == true)
{
foreach (var item in All)
item.Marked = true;
}
else
{
foreach (var item in All)
item.Marked = false;
}
Collection.View.Refresh();
注意:发件人是CheckBox