Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Select All check box together in WPF datagrid

my wpf data grid is,

<dg:DataGrid.Columns >
    <dg:DataGridTemplateColumn>
        <dg:DataGridTemplateColumn.Header>
            <CheckBox Content=" Slect All" Click="CheckBox_Click" />
        </dg:DataGridTemplateColumn.Header>
        <dg:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox Name="chkSelectAll"   Margin="45 2 0 0" Click="chkSelectAll_Click" />
            </DataTemplate>
        </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>
    <dg:DataGridTemplateColumn Header="Edit Row"  >
        <dg:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Edit" Click="Button_Click" />
            </DataTemplate>
        </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>
    <dg:DataGridTemplateColumn Header="Delete Row">
        <dg:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Delete" Click="DeleteButton_Click" />
            </DataTemplate>
        </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>
</dg:DataGrid.Columns>

on click of this Select all checkbox I need to get all checkbox as selected/Unselect. Can any one help me to get it done. Also one more thing , Dynamicaly I am populating the data to the datgrid.

-- thanks

like image 968
user3847840 Avatar asked Dec 25 '22 06:12

user3847840


1 Answers

Bind the checkBox IsChecked property to header checkBox using ElementName:

<dg:DataGridTemplateColumn>
    <dg:DataGridTemplateColumn.Header>
        <CheckBox Content=" Slect All" x:Name="headerCheckBox" />
    </dg:DataGridTemplateColumn.Header>
    <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox Name="chkSelectAll" Margin="45 2 0 0"
                      IsChecked="{Binding IsChecked, ElementName=headerCheckBox, 
                                          Mode=OneWay}" />
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
like image 149
Rohit Vats Avatar answered Dec 27 '22 20:12

Rohit Vats