首页 > 解决方案 > 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 中的那个不起作用。

谢谢

在此处输入图像描述

标签: c#wpfxaml

解决方案


“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”属性。


推荐阅读