WPF绑定命令到Datagrid组标题中的复选框

时间:2018-04-29 14:59:56

标签: c# wpf xaml

我尝试将命令绑定到数据网格的组头中的复选框。该复选框将检查/取消选中组中的所有项目。

当我将事件绑定到项目中的复选框时,它可以正常工作。但是当它绑定到组头时它不起作用。

<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中的那个不起作用。

由于

enter image description here

1 个答案:

答案 0 :(得分:1)

&#34; DataContent&#34;应该是&#34; DataContext&#34;在绑定路径中:

<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没有&#34; DataContent&#34;属性。