Maybe I did not understand your question, but do you mean something like this?
Binding binding = new Binding("SelectAll");
binding.Mode = BindingMode.TwoWay;
CheckBox headerCheckBox = new CheckBox();
headerCheckBox.Content = "Title";
headerCheckBox.SetBinding(CheckBox.IsCheckedProperty, binding);
DataGridCheckBoxColumn checkBoxColumn = new DataGridCheckBoxColumn();
checkBoxColumn.Header = headerCheckBox;
checkBoxColumn.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
checkBoxColumn.Binding = new Binding(e.PropertyName);
checkBoxColumn.IsThreeState = true;
I hope so.
EDIT
It is strange the behavoiur that you describe in your comment. However this is the complete project (I used .NET 4.0).
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="600">
<StackPanel>
<DataGrid Name="dataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Mode=OneWay, Path=Parties}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*" />
<DataGridTextColumn Header="Surname" Binding="{Binding Surname}" Width="*" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Window>
Model:
namespace WpfApplication1
{
public class Party : INotifyPropertyChanged
{
private bool isSelected;
private string name;
private string surname;
public event PropertyChangedEventHandler PropertyChanged;
public bool IsSelected
{
get
{
return isSelected;
}
set
{
if (isSelected != value)
{
isSelected = value;
OnPropertyChanged("IsSelected");
}
}
}
public string Name
{
get
{
return name;
}
set
{
if (name != value)
{
name = value;
OnPropertyChanged("Name");
}
}
}
public string Surname
{
get
{
return surname;
}
set
{
if (surname != value)
{
surname = value;
OnPropertyChanged("Surname");
}
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
ViewModel (you can improve the INotifyPropertyChanged implementation, by creating a base class):
namespace WpfApplication1
{
public class ViewModel : INotifyPropertyChanged
{
private bool selectAll;
private readonly ObservableCollection<Party> parties = new ObservableCollection<Party>();
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public ObservableCollection<Party> Parties
{
get
{
return parties;
}
}
public bool SelectAll
{
get
{
return selectAll;
}
set
{
if (selectAll != value)
{
selectAll = value;
OnPropertyChanged("SelectAll");
SelectAllImpl(selectAll);
}
}
}
private void SelectAllImpl(bool isSelected)
{
foreach (Party party in parties)
{
party.IsSelected = isSelected;
}
}
}
}
And then XAML code behind:
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ViewModel viewModel = new ViewModel();
viewModel.Parties.Add(new Party() { Name = "Joe", Surname = "Redgate" });
viewModel.Parties.Add(new Party() { Name = "Mike", Surname = "Blunt" });
viewModel.Parties.Add(new Party() { Name = "Gina", Surname = "Barber" });
DataContext = viewModel;
Binding binding = new Binding("DataContext.SelectAll");
binding.Mode = BindingMode.TwoWay;
binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor);
binding.RelativeSource.AncestorType = GetType();
CheckBox headerCheckBox = new CheckBox();
headerCheckBox.Content = "Is Selected";
headerCheckBox.SetBinding(CheckBox.IsCheckedProperty, binding);
DataGridCheckBoxColumn checkBoxColumn = new DataGridCheckBoxColumn();
checkBoxColumn.Header = headerCheckBox;
checkBoxColumn.Binding = new Binding("IsSelected");
dataGrid.Columns.Insert(0, checkBoxColumn);
}
}
}
And this is the result:
