全选活动:WPF Datagrid [英] Event for Select All: WPF Datagrid

查看:73
本文介绍了全选活动:WPF Datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF数据网格。在数据网格用户中,有列标题和行标题。

I am using WPF data-grid. In data-grid user have column-headers and row-headers.

当列标题和行标题都可见时,在左上角一小块正方形可用。 (列和行标题相交的左上角的横截面。)当我们单击它时,它将选择数据网格内的所有单元格。有什么事吗?如果没有,该如何捕获该事件。请指导我。

When column-headers and row-headers both of them are visible, in the top left corner we have one small square section available. (cross section in the top left corner where the column and row headers meet.) when we click on that it selects all the cells within data-grid. Is there any event for that? If not how can trap that event. Please guide me.

请告知我是否需要有关此问题的其他信息。

Do let me know if you need any other information regarding this problem.

请注意,
Priyank

Regards, Priyank

推荐答案

datagrid处理路由命令ApplicationCommand.SelectAll,因此,如果网格具有焦点并按Ctrl-A,或者单击角按钮,将选中所有单元格。
您可以通过在xaml中添加CommandBinding来自己处理此命令:

The datagrid handles the routed command ApplicationCommand.SelectAll, so if the grid has focus and your press Ctrl-A, or you click the corner button, all cells are selected. You can handle this command yourself by adding a CommandBinding in xaml:

<DataGrid x:Name="dataGrid" .../>
    <DataGrid.CommandBindings>
        <CommandBinding Command="ApplicationCommands.SelectAll" Executed="SelectAll_Executed"/>
    </DataGrid.CommandBindings>

或者您可以在代码中添加命令绑定:

Or you can add the command binding in code:

public MyControl(){
    InitializeComponent();
    ...
    dataGrid.CommandBindings.Add(new CommandBinding(ApplicationCommands.SelectAll, SelectAll_Executed));
}

但是,路由命令只能有一个处理程序,因此默认添加此处理程序,这将阻止全选在数据网格中工作。
在您的处理程序中,因此需要调用SelectAll。

However, there can only be a single handler for a routed command, so by default adding this handler this will prevent select all from working in the datagrid. In your handler you need therefore to call SelectAll.

private void SelectAll_Executed(object sender, ExecutedRoutedEventArgs e)
{
    Debug.WriteLine("Executed");
    dataGrid.SelectAll();
}

这篇关于全选活动:WPF Datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆