Search code examples
c#wpfwpfdatagrid

How could I access checkbox control placed in WPF datagrid columnHeaderStyle as ContentTemplate


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.


Solution

  • 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>