0

I have placed a checkbox control in an auto-generated WPF datagrid ColumnHeaderStyle as shown below:

<DataGrid.ColumnHeaderStyle>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <CheckBox x:Name="HeaderCheckBox" Content="{Binding}" />
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.ColumnHeaderStyle>

How could I access the CheckBox in code behind? There would be multiple columns in datagrid, how could I find out (column-wise) checkbox is selected or not ?
Please suggest.

DevX
  • 725
  • 3
  • 13
  • 26
  • [Find a WPF element inside DataTemplate in the code-behind](http://stackoverflow.com/questions/11826272/find-a-wpf-element-inside-datatemplate-in-the-code-behind) – franssu Jun 23 '14 at 10:52

2 Answers2

1

Just add the events Checked and Unchecked and once you checked it the event ll be raised

 <DataGrid.ColumnHeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <CheckBox x:Name="HeaderCheckBox" Content="{Binding}"
                                 Checked="CheckBoxChanged" Unchecked="CheckBoxChanged"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
    </DataGrid.ColumnHeaderStyle>
MRebai
  • 5,344
  • 3
  • 33
  • 52
  • Thanks moez for the response. I have one more query, my datagrid have some readonly columns and I don't want to show checkbox in header for these columns, how could I achieve this? – DevX Jun 23 '14 at 11:22
  • ok, First if this answer works fine plz make it as answer, then to fix your issue you just need to add a DataTemplate.Triggers to your column datatemplate – MRebai Jun 23 '14 at 13:07
  • Thanks moez.I have marked your response as answer. Your response was helpful. I tried to use DataTemplate.Triggers but didn't find IsReadOnly property to check in this context. Could you please help, how to use triggers to hide checkbox for readonly columns. – DevX Jun 23 '14 at 14:59
1

Just implement Checked and Unchecked events

  <DataGrid.ColumnHeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <CheckBox x:Name="HeaderCheckBox" Content="{Binding}"
                                     Checked="CheckBoxChanged" Unchecked="CheckBoxChanged"/>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
        </DataGrid.ColumnHeaderStyle>

     private void CheckBoxChanged(object sender, RoutedEventArgs e)
     {
                // add what you want
     }