WPF 数据网格“全选"按钮 - “取消全选"也? [英] WPF Datagrid "Select All" button - "Unselect All" too?

查看:19
本文介绍了WPF 数据网格“全选"按钮 - “取消全选"也?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以向数据网格左上角的全选"按钮添加功能,以便它也取消选择所有行?我有一个附加到按钮的方法可以执行此操作,但是如果我可以从全选"按钮触发此方法以将功能保留在视图的同一部分,那就太好了.这个全选"按钮是否可以添加代码,如果是这样,如何到达按钮?我找不到任何示例或建议.

I would like to know if it would be possible to add functionality to the 'Select All' button in the top left of a datagrid so that it also unselects all rows? I have a method attached to a button which does this, but it would be great if I could fire this method from the Select All button, to keep functionality in the same part of the view. Can this 'Select All' button have code added to it, and if so, how would one get to the button? I haven't been able to find any examples or suggestions.

推荐答案

好的,经过大量搜索,我从 Colin Eberhardt 那里找到了如何转到按钮的方法,在这里:

OK after a lot of searching I found out how to do get to the button from Colin Eberhardt, here:

在具有附加行为的控制模板中对难以触及的元素进行样式设置

然后我在他的类中扩展了Grid_Loaded"方法,给按钮添加了一个事件处理程序,但是记得先把默认的'全选'命令去掉(否则,运行我们添加的事件处理程序后,命令会得到跑).

Then I extended the "Grid_Loaded" method in his class to add an event handler to the button, but remember to remove the default 'Select All' command first (otherwise, after running the event handler we have added, the command gets run).

/// <summary>
/// Handles the DataGrid's Loaded event.
/// </summary>
/// <param name="sender">Sender object.</param>
/// <param name="e">Event args.</param>
private static void Grid_Loaded(object sender, RoutedEventArgs e)
{
    DataGrid grid = sender as DataGrid;
    DependencyObject dep = grid;

    // Navigate down the visual tree to the button
    while (!(dep is Button))
    {
        dep = VisualTreeHelper.GetChild(dep, 0);
    }

    Button button = dep as Button;

    // apply our new template
    ControlTemplate template = GetSelectAllButtonTemplate(grid);
    button.Template = template;
    button.Command = null;
    button.Click += new RoutedEventHandler(SelectAllClicked);
}

/// <summary>
/// Handles the DataGrid's select all button's click event.
/// </summary>
/// <param name="sender">Sender object.</param>
/// <param name="e">Event args.</param>
private static void SelectAllClicked(object sender, RoutedEventArgs e)
{
    Button button = sender as Button;
    DependencyObject dep = button;

    // Navigate up the visual tree to the grid
    while (!(dep is DataGrid))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }

    DataGrid grid = dep as DataGrid;

    if (grid.SelectedItems.Count < grid.Items.Count)
    {
        grid.SelectAll();
    }
    else
    {
        grid.UnselectAll();
    }

    e.Handled = true;
}

本质上,如果有任何行没有被选中,它就会全选",否则就取消全选".它的工作原理与您期望的全选/取消全选工作非常相似,老实说,我不敢相信他们没有让命令默认执行此操作,也许在下一个版本中.

Essentially, if any rows are not selected it 'selects all', if not it 'unselects all'. It works pretty much like you would expect a select/unselect all to work, I can't believe they didn't make the command do this by default to be honest, maybe in the next release.

无论如何希望这对某人有所帮助,干杯,将

Hope this helps somebody anyway, Cheers, Will

这篇关于WPF 数据网格“全选"按钮 - “取消全选"也?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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