Turns out it's a lot harder than you can hope for.
The first problem is that you cannot just bind the view model to the column heading because it does not have the view model as its data context, so you need a connecting proxy to properly direct the binding to the view model.
public class BindingProxy : Freezable { public static readonly DependencyProperty DataProperty = DependencyProperty.Register( "Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null)); public object Data { get { return this.GetValue(DataProperty); } set { this.SetValue(DataProperty, value); } } protected override Freezable CreateInstanceCore() { return new BindingProxy(); } }
Now create a binding proxy in the data grid resources:
<DataGrid.Resources> <aon:BindingProxy x:Key="DataContextProxy" Data="{Binding}" /> </DataGrid.Resources>
Then the column should be defined as:
<DataGridTemplateColumn> <DataGridTemplateColumn.HeaderTemplate> <DataTemplate> <CheckBox Command="{Binding Data.SelectAllCommand, Source={StaticResource DataContextProxy}}" IsChecked="{Binding Data.AreAllSelected, Mode=OneWay, Source={StaticResource DataContextProxy}, UpdateSourceTrigger=PropertyChanged}" IsThreeState="True" /> </DataTemplate> </DataGridTemplateColumn.HeaderTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
Note that you need to bind both the IsChecked dependency property and the Command property, and the IsChecked binding IsChecked be OneWay . The IsChecked binding receives a flag to display the current state of the elements, and the Command binding selects the mass. You need both.
Now in the view model:
public bool? AreAllSelected { get { return this.Items.All(candidate => candidate.IsSelected) ? true : this.Items.All(candidate => !candidate.IsSelected) ? (bool?)false : null; } set { if (value != null) { foreach (var item in this.Items) { item.IsSelected = value.Value; } } this.RaisePropertyChanged(); } }
And the SelectAllCommand property is an implementation of ICommand , where the Execute method:
public void Execute(object parameter) { var allSelected = this.AreAllSelected; switch (allSelected) { case true: this.AreAllSelected = false; break; case false: case null: this.AreAllSelected = true; break; } }
Finally, your line item position model (i.e., things in Items ) should raise the PropertyChanged in the main view model every time the IsSelected value IsSelected . How you do this is largely up to you.