简体   繁体   中英

WPF DataGrid CheckBox Select All And Get Selected Rows

I have a Datagrid in WPF. I added a checkbox column as follows:

    <DataGridTemplateColumn>
     <DataGridTemplateColumn.Header>
       <CheckBox Name="CheckALL"  IsChecked="{Binding IsCheckedAll, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}" />
     </DataGridTemplateColumn.Header>
     <DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
          <CheckBox Name="chkSelect" IsChecked="{Binding IsSelected, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}"/>
       </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
 </DataGridTemplateColumn>

Now property changed event gets fired on check box in rows is clicked, But I also want to get all check boxes checked when header check box is clicked.

When Header check box is clicked, All checkboxes are selected when i write code like this:

<DataTemplate>
 <CheckBox Name="chkSelect" IsChecked="{Binding IsChecked, Mode=OneWay, ElementName=CheckALL,  UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>

But at the same time both functions are to be obtained. Please help me

Try something like this:

<Checkbox Name="CheckALL">
 <i:Interaction.Triggers>
    <i:EventTrigger EventName="SelectionChanged">
        <i:InvokeCommandAction Command="{Binding CheckColumns}" CommandParameter="{Binding ElementName=hereNameOfGrid}"/>
     </i:EventTrigger>
   </i:Interaction.Triggers>
</Checkbox>

now u need a command in your code which could look like this:

public void CheckColumns(object _grid)
{
        GridView tempGrid = _grid as GridView;

        foreach (var item in tempGrid.Columns)
        {
            item.IsChecked = false;
        }    
}

Something like that should work. This is not working code, its just a hint to a solution!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM