Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

CheckBox in DataGrid Header to Check or UnCheck CheckBoxes in all DataGrid Items

$
0
0

We have an MVVM application with what should be a simple requirement.  You've all seen this in the wild.  You check the "header" checkbox and all "detail" checkboxes become checked.  Ditto for un-checking.  Theoretically, if you manually check all items, the "header" checkbox should also become checked.

Ours is a little bit unusual, in that if ANY of the detail items are checked, they want the "header" checkbox to become checked.  Therefore, it is inconsistent, but that's what they want.  Please don't address this particular detail, as I'm trying to convince them that it should be more consistent and when ALL details are checked it should automatically check the "header" checkbox, and un-checking even one "detail" checkbox should un-check the "header" checkbox.

That being said, the XAML for the checkbox inside the header template is like this:

<CheckBox IsChecked="{Binding DataContext.PmSearchSelected, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding DataContext.SelectAllPmSearchesCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
</i:EventTrigger>
... there is another event trigger for "Unchecked" here...
</i:Interaction.Triggers>
</Checkbox>

BTW, "i" is namespace for System.Windows.Interactivity.

In the VM:

The SelectAllPmSearches method does this:
Model.ForEach(c => c.IsSelected = true);

The RemoveAllPmSearches method is the same, except sets IsSelected = false.

The PmSearchSelectionChanged method does this:
if (pmSearch.IsSelected)
{
PmSearchSelected = true;
}
else
{
if (Model.Any(c => c.IsSelected))
PmSearchSelected = false;
}

I think that's everything pertinent.  Whew...

The problem is that the moment I check a "detail" checkbox, it makes the "header" checkbox checked, which causes ALL of the "detail" checkboxes to be checked.

Isn't there a way to  set the "header" checkbox programmatically without firing the code to check all of the "detail" checkboxes?  IOW, I only want that code to fire if I manually check the "header" checkbox, not if it is checked programmatically.

All of this code was already written.  Is there perhaps a better way to achieve the desired result?

Thanks!!!!


Ron Mittelman


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>