views:

1549

answers:

1

Hi,

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.

Thanks,

Will

+1  A: 

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

Styling hard-to-reach elements in control templates with attached behaviours

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

WillH
Very useful thanks!
Klerk
Thanks - The code sample would be complete if the definition for 'GetSelectAllButtonTemplate()' were included.
PandaWood