I make a DataGrid have DataGridCheckBoxColumn. I want to binding the CheckedAll property of DataContext to IsChecked of checkbox in DataGridCheckBoxColumn.HeaderTemplate.
Here is my Data row item class:
public class ContractForMuster : INotifyPropertyChanged
{
private bool _Checked;
public bool Checked
{
get
{
return _Checked;
}
set
{
_Checked = value;
OnPropertyChanged("Checked");
}
}
public int ID { get; set; }
public string ContractCode { get; set; }
public string Fullname { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Here is my DataContext class:
public class ContractForMusters : INotifyPropertyChanged
{
private bool _CheckedAll;
public bool CheckedAll
{
get
{
return _CheckedAll;
}
set
{
_CheckedAll = value;
OnPropertyChanged("CheckedAll");
}
}
public List<ContractForMuster> models { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Code behide:
viewModel = new ContractForMusters();
entities = new List<ContractForMuster>();
entities.Add(entity); //Code add
viewModel.models = entities;
viewModel.CheckedAll = true;
this.DataContext = viewModel;
And XAML code:
<DataGrid Name="DgdContract" Style="{StaticResource DataGridStyle}" ItemsSource="{Binding models}">
<DataGrid.Columns>
<DataGridCheckBoxColumn MaxWidth="50" MinWidth="30" Binding="{Binding Checked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate>
<CheckBox x:Name="ChkAll" HorizontalAlignment="Center" Click="DgdContractCheckBoxItemChkAll_Click" IsChecked="{Binding ?}" />
</DataTemplate>
</DataGridCheckBoxColumn.HeaderTemplate>
<DataGridCheckBoxColumn.ElementStyle>
<Style>
<Setter Property="Control.VerticalAlignment" Value="Center" />
<Setter Property="Control.HorizontalAlignment" Value="Center" />
<Setter Property="Control.Margin" Value="5 0 0 0" />
<EventSetter Event="CheckBox.Click" Handler="DgdContractCheckBoxItem_Click" />
</Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
<DataGridTextColumn ElementStyle="{StaticResource IDAlignmentAlign}" Width="Auto" Binding="{Binding ID}" Header="ID" HeaderStyle="{StaticResource ColumnHeaderStyle}" />
<DataGridTextColumn ElementStyle="{StaticResource CellAlignmentAlign}" Binding="{Binding ContractCode}" Header="Contract Code" HeaderStyle="{StaticResource ColumnHeaderStyle}" />
<DataGridTextColumn ElementStyle="{StaticResource CellAlignmentAlign}" Binding="{Binding Fullname}" Header="Fullname" HeaderStyle="{StaticResource ColumnHeaderStyle}" />
</DataGrid.Columns>
</DataGrid>
How to can I binding property CheckedAll to IsChecked of checkbox ChkAll?
Thanks!