WPF的Datagrid"所有&QUOT选择;按钮 - "全部取消"太? [英] WPF Datagrid "Select All" button - "Unselect All" too?

查看:163
本文介绍了WPF的Datagrid"所有&QUOT选择;按钮 - "全部取消"太?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能将功能添加到了全选按钮,在数据网格的左上角,以便它也取消选择所有行?我有连接到一个按钮,做到这一点的方法,但是这将是巨大的,如果我可以从选择触发此方法的所有按钮,保持功能在视图中的相同部分。可这全选按钮,有code添加到它,如果是的话,如何能到达按钮?我一直没能找到任何实例或建议。

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.

推荐答案

确定后,很多搜索我发现了怎么办得到科林埃伯哈特,这里的按钮:

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

<一个href="http://www.scottlogic.co.uk/blog/wpf/2009/02/styling-hard-to-reach-elements-in-control-templates-with-attached-behaviours">Styling难以触及的附加行为的控制模板元素

然后,我在他的课延长了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;
        }

从本质上讲,如果任何行不选择它选择所有,如果不是未选中所有。它的工作原理pretty的就像你所期望的选择/取消选择所有的工作,我不能相信他们没有命令做到这一点,默认情况下,说实话,也许在下一版本中。

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的Datagrid&QUOT;所有&QUOT选择;按钮 - &QUOT;全部取消&QUOT;太?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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