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!

Son Le
  • 229
  • 5
  • 16
  • I had found a a method to solve this problem here: http://stackoverflow.com/questions/7594438/bind-to-parent-datacontext-within-datatemplate – Son Le Jan 16 '15 at 09:35

2 Answers2

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}" />
robertos
  • 1,810
  • 10
  • 12
  • 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. – Son Le Jan 16 '15 at 04:31
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!

Son Le
  • 229
  • 5
  • 16