Reputation: 43
I tried to bind the command to the checkbox in the group header for the data grid. The checkbox will do check/un-check all the items in the group.
When I bind the event to checkbox in the item, it works fine. But it does not work when bind it to the group header.
<DataGrid ItemsSource="{Binding GroupClients}" CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False">
<!-- Define the group style -->
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="True" Margin="0,8,4,0" FontSize="22">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding DataContent.GroupHeaderEventHandler, RelativeSource={RelativeSource AncestorType=DataGrid}}" CommandParameter="Name"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" FontSize="22"></TextBlock>
<TextBlock Text=": " FontSize="22"></TextBlock>
<TextBlock Text="{Binding Path=ItemCount}" FontSize="22" Margin="4,0,4,0" Foreground="Green" FontWeight="Bold" FontStyle="Italic"></TextBlock>
<TextBlock Text="Files" Foreground="Silver" FontSize="22" FontStyle="Italic"></TextBlock>
</StackPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<!-- Columns defined -->
<DataGrid.Columns>
<!-- Selected Column -->
<DataGridTemplateColumn Header="Selected" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=Selected}" HorizontalAlignment="Center" VerticalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding DataContext.GroupHeaderEventHandler, RelativeSource={RelativeSource AncestorType=DataGrid}}" CommandParameter="Index"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Name Column -->
<DataGridTemplateColumn Header="File Name" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Version Column -->
<DataGridTemplateColumn Header="Version" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Version}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
From the XAML, I tried to bind the command to the checkbox, but it only work in the DataGridColumn, the one in Expander.Header does not work.
Thanks
Upvotes: 1
Views: 1633
Reputation: 169190
"DataContent" should be "DataContext" in the binding path:
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="True" Margin="0,8,4,0" FontSize="22">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding DataContext.GroupHeaderEventHandler,
RelativeSource={RelativeSource AncestorType=DataGrid}}"
CommandParameter="Name" />
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" FontSize="22"></TextBlock>
<TextBlock Text=": " FontSize="22"></TextBlock>
<TextBlock Text="{Binding Path=ItemCount}" FontSize="22" Margin="4,0,4,0" Foreground="Green" FontWeight="Bold" FontStyle="Italic"></TextBlock>
<TextBlock Text="Files" Foreground="Silver" FontSize="22" FontStyle="Italic"></TextBlock>
</StackPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
The DataGrid
has no "DataContent" property.
Upvotes: 2