如何在单击数据网格中的复选框列标题时显示弹出菜单?

发布于 2024-07-27 14:14:53 字数 1674 浏览 0 评论 0原文

我在 WPF Windows 应用程序中使用 WPF 工具包中的数据网格。 它的行标题中有一个数据模板,它创建一个用于选择行的复选框列。

如果单击复选框列的标题行(网格中最左上角的单元格),它将选中网格中的所有复选框,从而选择所有行。

xaml 中的相关部分

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" >

<toolkit:DataGrid Name="dataGrid" ItemsSource="{Binding}" 
AutoGenerateColumns="True" SelectionMode="Extended" CanResizeRows="False">
    <toolkit:DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <Grid>
                <CheckBox IsChecked="{
                Binding Path=IsSelected, 
                Mode=TwoWay, 
                RelativeSource={RelativeSource FindAncestor, 
                AncestorType={x:Type toolkit:DataGridRow}}}"
                />
            </Grid>
        </DataTemplate>
    </toolkit:DataGrid.RowHeaderTemplate>
</toolkit:DataGrid>
</Window>

现在我想知道如何处理此点击。 我计划处理这个问题并显示弹出菜单

我应该为此连接哪个控件的单击事件?

答案: 使用 SelectAllCommand 来处理事件,如下所示(示例)

datagrid.CommandBindings.Add(new CommandBinding(
Microsoft.Windows.Controls.DataGrid.SelectAllCommand, 
OnSelectAll, CanExecuteSelectAll));
public void OnSelectAll(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("All Selected");
    }
public void CanExecuteSelectAll(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
        Console.WriteLine("Can I execute?");
    }

I am using the datagrid from WPF toolkit in a WPF windows application.
It has a datatemplate in the rowheader which creates a checkbox column used for selecting the rows.

If you click on the header row of the checkbox column (the top leftmost cell in the grid), it will check all the checkboxes in the grid thereby selecting all the rows.

Relevant portions from the xaml

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" >

<toolkit:DataGrid Name="dataGrid" ItemsSource="{Binding}" 
AutoGenerateColumns="True" SelectionMode="Extended" CanResizeRows="False">
    <toolkit:DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <Grid>
                <CheckBox IsChecked="{
                Binding Path=IsSelected, 
                Mode=TwoWay, 
                RelativeSource={RelativeSource FindAncestor, 
                AncestorType={x:Type toolkit:DataGridRow}}}"
                />
            </Grid>
        </DataTemplate>
    </toolkit:DataGrid.RowHeaderTemplate>
</toolkit:DataGrid>
</Window>

Now I want to know how to handle this click. I plan to handle that and show a popup menu instead.

Which control's click event should I wire for this?

Answer:
using SelectAllCommand to handle the events like so (example)

datagrid.CommandBindings.Add(new CommandBinding(
Microsoft.Windows.Controls.DataGrid.SelectAllCommand, 
OnSelectAll, CanExecuteSelectAll));
public void OnSelectAll(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("All Selected");
    }
public void CanExecuteSelectAll(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
        Console.WriteLine("Can I execute?");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2024-08-03 14:14:53

如果展开 DataGrid ControlTemplate,您可以看到有一个绑定到 SelectAllCommand 的按钮。

<Window x:Class="WpfApplication1.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Window3" Height="500" Width="500"
Loaded="Window_Loaded">
<Window.Resources>
    <Style x:Key="DataGridStyle1" TargetType="{x:Type toolkit:DataGrid}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderBrush" Value="#FF688CAF"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type toolkit:DataGrid}">
                    <Border SnapsToDevicePixels="True" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <ScrollViewer x:Name="DG_ScrollViewer" Focusable="False">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Button Command="{x:Static toolkit:DataGrid.SelectAllCommand}"
                                         Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}}" Focusable="False">
                                            <Button.Template>
                                                <ControlTemplate TargetType="{x:Type Button}">
                                                    <Grid>
                                                        <Rectangle x:Name="Border" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" SnapsToDevicePixels="True"/>
                                                        <Polygon x:Name="Arrow" Fill="Black" Stretch="Uniform" HorizontalAlignment="Right" Margin="8,8,3,3" VerticalAlignment="Bottom" Opacity="0.15" Points="0,10 10,10 10,0"/>
                                                    </Grid>
                                                    <ControlTemplate.Triggers>
                                                        <Trigger Property="IsMouseOver" Value="True">
                                                            <Setter Property="Stroke" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                                                        </Trigger>
                                                        <Trigger Property="IsPressed" Value="True">
                                                            <Setter Property="Fill" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                                                        </Trigger>
                                                        <Trigger Property="IsEnabled" Value="False">
                                                            <Setter Property="Visibility" TargetName="Arrow" Value="Collapsed"/>
                                                        </Trigger>
                                                    </ControlTemplate.Triggers>
                                                </ControlTemplate>
                                            </Button.Template>
                                            <Button.Visibility>
                                                <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}">
                                                    <Binding.ConverterParameter>
                                                        <toolkit:DataGridHeadersVisibility>All</toolkit:DataGridHeadersVisibility>
                                                    </Binding.ConverterParameter>
                                                </Binding>
                                            </Button.Visibility>
                                        </Button>
                                        <toolkit:DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1">
                                            <toolkit:DataGridColumnHeadersPresenter.Visibility>
                                                <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}">
                                                    <Binding.ConverterParameter>
                                                        <toolkit:DataGridHeadersVisibility>Column</toolkit:DataGridHeadersVisibility>
                                                    </Binding.ConverterParameter>
                                                </Binding>
                                            </toolkit:DataGridColumnHeadersPresenter.Visibility>
                                        </toolkit:DataGridColumnHeadersPresenter>
                                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" ContentTemplate="{TemplateBinding ContentTemplate}" Grid.ColumnSpan="2" Grid.Row="1" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False"/>
                                        <ScrollBar x:Name="PART_VerticalScrollBar" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Grid.Column="2" Grid.Row="1" Maximum="{TemplateBinding ScrollableHeight}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                        <Grid Grid.Column="1" Grid.Row="2">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}}"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <ScrollBar x:Name="PART_HorizontalScrollBar" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsGrouping" Value="True">
                <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <toolkit:DataGrid Name="dataGrid" 
                      ItemsSource="{Binding}"
                      CanUserResizeRows="False" 
                      SelectionMode="Extended" 
                      AutoGenerateColumns="True" Style="{DynamicResource DataGridStyle1}">
        <toolkit:DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <Grid>
                    <CheckBox IsChecked="{
                        Binding Path=IsSelected, 
                        Mode=TwoWay, 
                        RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type toolkit:DataGridRow}}}"
                        />
                </Grid>
            </DataTemplate>
        </toolkit:DataGrid.RowHeaderTemplate>
    </toolkit:DataGrid>
</Grid>

您可以覆盖该按钮的命令来执行您自己的逻辑,甚至可以用不同的控件替换该按钮。

If you expand the DataGrid ControlTemplate you can see that there is a Button which is bound to the SelectAllCommand.

<Window x:Class="WpfApplication1.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Window3" Height="500" Width="500"
Loaded="Window_Loaded">
<Window.Resources>
    <Style x:Key="DataGridStyle1" TargetType="{x:Type toolkit:DataGrid}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderBrush" Value="#FF688CAF"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type toolkit:DataGrid}">
                    <Border SnapsToDevicePixels="True" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <ScrollViewer x:Name="DG_ScrollViewer" Focusable="False">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Button Command="{x:Static toolkit:DataGrid.SelectAllCommand}"
                                         Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}}" Focusable="False">
                                            <Button.Template>
                                                <ControlTemplate TargetType="{x:Type Button}">
                                                    <Grid>
                                                        <Rectangle x:Name="Border" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" SnapsToDevicePixels="True"/>
                                                        <Polygon x:Name="Arrow" Fill="Black" Stretch="Uniform" HorizontalAlignment="Right" Margin="8,8,3,3" VerticalAlignment="Bottom" Opacity="0.15" Points="0,10 10,10 10,0"/>
                                                    </Grid>
                                                    <ControlTemplate.Triggers>
                                                        <Trigger Property="IsMouseOver" Value="True">
                                                            <Setter Property="Stroke" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                                                        </Trigger>
                                                        <Trigger Property="IsPressed" Value="True">
                                                            <Setter Property="Fill" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                                                        </Trigger>
                                                        <Trigger Property="IsEnabled" Value="False">
                                                            <Setter Property="Visibility" TargetName="Arrow" Value="Collapsed"/>
                                                        </Trigger>
                                                    </ControlTemplate.Triggers>
                                                </ControlTemplate>
                                            </Button.Template>
                                            <Button.Visibility>
                                                <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}">
                                                    <Binding.ConverterParameter>
                                                        <toolkit:DataGridHeadersVisibility>All</toolkit:DataGridHeadersVisibility>
                                                    </Binding.ConverterParameter>
                                                </Binding>
                                            </Button.Visibility>
                                        </Button>
                                        <toolkit:DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1">
                                            <toolkit:DataGridColumnHeadersPresenter.Visibility>
                                                <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}">
                                                    <Binding.ConverterParameter>
                                                        <toolkit:DataGridHeadersVisibility>Column</toolkit:DataGridHeadersVisibility>
                                                    </Binding.ConverterParameter>
                                                </Binding>
                                            </toolkit:DataGridColumnHeadersPresenter.Visibility>
                                        </toolkit:DataGridColumnHeadersPresenter>
                                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" ContentTemplate="{TemplateBinding ContentTemplate}" Grid.ColumnSpan="2" Grid.Row="1" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False"/>
                                        <ScrollBar x:Name="PART_VerticalScrollBar" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Grid.Column="2" Grid.Row="1" Maximum="{TemplateBinding ScrollableHeight}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                        <Grid Grid.Column="1" Grid.Row="2">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type toolkit:DataGrid}}}"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <ScrollBar x:Name="PART_HorizontalScrollBar" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsGrouping" Value="True">
                <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <toolkit:DataGrid Name="dataGrid" 
                      ItemsSource="{Binding}"
                      CanUserResizeRows="False" 
                      SelectionMode="Extended" 
                      AutoGenerateColumns="True" Style="{DynamicResource DataGridStyle1}">
        <toolkit:DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <Grid>
                    <CheckBox IsChecked="{
                        Binding Path=IsSelected, 
                        Mode=TwoWay, 
                        RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type toolkit:DataGridRow}}}"
                        />
                </Grid>
            </DataTemplate>
        </toolkit:DataGrid.RowHeaderTemplate>
    </toolkit:DataGrid>
</Grid>

You can override that Button's Command to perform your own logic or even replace the button with a different control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文