Reputation: 12843
I've a DataGrid that is bound to a Datatable object. DataGrid generates columns automatically.
<DataGrid
Name="TimeTableDataGrid"
AutoGeneratingColumn="TimeTableDataGrid_OnAutoGeneratingColumn"
ItemsSource="{Binding TimeTable,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,IsAsync=True}"
EnableRowVirtualization="True"
EnableColumnVirtualization="True"
VirtualizingStackPanel.IsVirtualizing="True">
</DataGrid>
I've use AutoGeneratingColumn event to add checkbox in DataGrid columns .
private void TimeTableDataGrid_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Column.Width = new DataGridLength(_columnWidth);
e.Column.HeaderTemplate = (DataTemplate) Resources["HeaderTemplate"];
}
HeaderTemplate :
<DataTemplate x:Key="HeaderTemplate"
x:Name="HeaderTemplate">
<CheckBox></CheckBox>
</DataTemplate>
How can I determine that which checkboxes are selected ?
Upvotes: 0
Views: 477
Reputation: 18580
you can create the style for your header and update your checkbox like below:
<Style x:Key="HeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<CheckBox Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{TemplateBinding Content}"></CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
and update your autogenererating handler as :
e.Column.Width = new DataGridLength(_columnWidth);
e.Column.Header = e.PropertyName;
e.Column.HeaderStyle = (Style)Resources["HeaderStyle"];
so here you can bind the Command for checkbox to your viewmodel command and send the unique commandparameter that can be header. In viewmodel you can have list (of string). In the command handler you can upate that list to contain which header's checkbox is checked/unchecked
Thanks
Upvotes: 1