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.

1

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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
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.
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
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.