Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the 'Select All' button of a DataGrid

Is it possible to disable the "Select all" button in the upper left corner of the WPF DataGrid?

like image 746
urema Avatar asked May 12 '10 10:05

urema


3 Answers

There is a Property HeadersVisibility in DataGrid. It has four values - All, Column, Row, None.

With HeadersVisibility = All, you will get the SelectAll Button.

With HeadersVisibility = Column, you will get only Columns. Not the SelectAll Button or Row Headers to select a complete row.

With HeadersVisibility = Row, you will get only Row headers to select whole row. Not the SelectAll Button or Columns.

With HeadersVisibility = None, you will get nothing. All the headers will be hidden.

I hope this helps you.

like image 196
Varun Jain Avatar answered Oct 16 '22 01:10

Varun Jain


After using Snoop to analyze the Visual Tree of a test app I put together, I came up with this solution using the DataGrid_Loaded event):

private void TheGrid_Loaded(object sender, RoutedEventArgs e) {
    var dataGrid = (DataGrid)sender;
    var border = (Border)VisualTreeHelper.GetChild(dataGrid, 0);
    var scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
    var grid = (Grid)VisualTreeHelper.GetChild(scrollViewer, 0);
    var button = (Button)VisualTreeHelper.GetChild(grid, 0);
    button.IsEnabled = false;
}

There may be a more elegant XAML only solution out there, but this is what came to mind first, and it seems to work well enough (I'm obviously not doing any Exception handling either).

Note: I haven't played around with disabling/re-enabling the DataGrid to make sure that the select all button stays disabled. If it doesn't stay disabled, then you may want to also hook into the DataGrid_IsEnabledChanged event.

Hope this helps!!

like image 39
Mark Carpenter Avatar answered Oct 16 '22 02:10

Mark Carpenter


Add a commandbinding to the SelectAll Command and return false in the CanExecute to disable the selectall button.

see: Event for Select All: WPF Datagrid

like image 41
Eben Avatar answered Oct 16 '22 02:10

Eben