monstr
monstr

Reputation: 1730

DataGridColumnHeader CheckBox IsChecked Binding doesn't work

It is all - part of the UI, INotifyPropertyChanged interface of cource implemented, all Bindings work fine. But by some reason one Binding does't work. Here it is.

IsChecked="{Binding ArchiveDocsLinkedChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >

This is my XAML:

<DataGridTemplateColumn Width="Auto">
   <DataGridTemplateColumn.HeaderStyle>
      <Style TargetType="{x:Type DataGridColumnHeader}">
         <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                  <CheckBox Margin="0,2,3,0" Checked="ArchiveDocsLinkedMainCheckBoxChecked"  Unchecked="ArchiveDocsLinkedMainCheckBoxUnchecked" HorizontalAlignment="Center"  IsChecked="{Binding ArchiveDocsLinkedChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
                     <CheckBox.ToolTip>Выделить все/Снять выделение</CheckBox.ToolTip>
                  </CheckBox>
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>
   </DataGridTemplateColumn.HeaderStyle>
   <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" Name="theCheckbox"/>
      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>   

And this is my code-behind field + property:

private Boolean _archiveDocsLinkedChecked
public Boolean ArchiveDocsLinkedChecked
{
    get
    {
        return _archiveDocsLinkedChecked;
    }
    set
    {
        _archiveDocsLinkedChecked = value;
        RaisePropertyChanged("ArchiveDocsLinkedChecked");
    }
}        

I suppose it is because of Binding is within Template of DataGridColumnHeader. And it is not pretty standart situation for bindings. How can I specify that Binding properly? thx!

Upvotes: 1

Views: 932

Answers (1)

monstr
monstr

Reputation: 1730

I solve the problem.

Binding must looks like this:

IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, 
                                                                Path=ArchiveDocsLinkedChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

Obviously, when you are in ControlTemplate you are within DataContext of ControlTemplate. So you must go upper to get access to Window's DataContext :)

p.s. why, when I copy&paste some code it appears shifted to right side? ctrl+K doesn't solve that, and I forced adjust code manually...

Upvotes: 1

Related Questions