c# - WPF 绑定命令到 Datagrid 组标题中的复选框
问题描述
我试图将命令绑定到数据网格组标题中的复选框。该复选框将选中/取消选中组中的所有项目。
当我将事件绑定到项目中的复选框时,它工作正常。但是将它绑定到组头时它不起作用。
<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>
从 XAML 中,我尝试将命令绑定到复选框,但它只在 DataGridColumn 中有效,在 Expander.Header 中的那个不起作用。
谢谢
解决方案
“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>
DataGrid
没有“DataContent”属性。
推荐阅读
- python - 如何使用字符和数字字典绘制饼图
- javascript - Javascript Canvas 如何从图像中删除形状或区域?
- android - 隐私政策发布
- c# - AutoMapper:如何将对象集合映射到 Pair
收藏 - javascript - ajax调用后猫头鹰轮播跳转到特定索引
- javascript - 如何在 javascript 中添加 gridColumnStart 和 gridRowStart?
- ruby-on-rails - Ruby on Rails - Faraday SSLError 错误的版本号
- java - 一对多关系时使用spring存储库获取数据
- javascript - 触发器在谷歌应用脚本上随机启动
- java - TableView 不显示内容