[英]WPF Binding command to checkbox in Datagrid group header
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. 从XAML,我尝试将命令绑定到复选框,但它仅在DataGridColumn中起作用,Expander.Header中的命令不起作用。
Thanks 谢谢
"DataContent" should be "DataContext" in the binding path: 绑定路径中的“ DataContent”应为“ DataContext”:
<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. DataGrid
没有“ DataContent”属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.