0

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!

1

2 Answers 2

0

You can bind directly to the datacontext of the root. Something like this (untested):

(Root of your document)

<Window ... x:Name="root"> 

(Your element)

<CheckBox x:Name="ChkAll" HorizontalAlignment="Center" Click="DgdContractCheckBoxItemChkAll_Click" DataContext="{Binding ElementName=root}" IsChecked="{Binding CheckedAll}" /> 
Sign up to request clarification or add additional context in comments.

1 Comment

But if I using root property, I can't using INotifyPropertyChanged for CheckAll property. Ofcourse, I can find control ChkAll with code and set check for it, but I don't want using this method.
0

I have solved my problem.

In Window element, add x:Name="root", then in checkbox:

<CheckBox x:Name="ChkAll" HorizontalAlignment="Center" Click="DgdContractCheckBoxItemChkAll_Click" IsChecked="{Binding DataContext.CheckedAll, ElementName=root}" /> 

Thanks All!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.