WPF DataGrid: Changing the style of the SelectAll Button

Jan 21, 2009 at 7:36 AM

I am looking for some sample code for changing the style of the Select All button in the upper-left corner of the WPF DataGrid.

I have applied a new style for columns and rows but the Select All button remains unchanged.
I don't know how to change it.

There are some good examples that I don't understand completely.

Every effort to help me is appreciated.

Thanks in advance.

Jan 21, 2009 at 1:06 PM
There isn't any kind of convenience APIs on DataGrid to customize this style.  You ethier have to create an implicit style on Button or create a copy of the DataGrid template and update the select all button template to point to your own custom template.
Jan 21, 2009 at 2:38 PM
I created a custom style and applied it in Loaded event handler of the DataGrid  but after a second my styling disappeared and returned the original one.

When or where I have to place a code to change a style?

Or maybe that button is of special type like DataGridColumnHeader or DataGridRowHeader so I may add my styling in the resources section of the XAML
using that special TargetType ?
Jan 21, 2009 at 3:56 PM

Firstly, you should look at the Generic.xaml within the WPF Toolkit to understand exactly what it is you are styling.

You have a few options ...

(1) Implicit style on Button - this is probably not what you want? Do you want all your buttons everywhere to look the same?
(2) Change the DataGrid template - this will unfortunately result in a lot of cut and paste code as you will have to reproduce all of the templates / styles that the DataGrid uses.
(3) Handle the Loaded event and style the Button dynamically.

Here is an example of #3 ...

Handle the Loaded event for your DataGrid as follows:

private void Grid_Loaded(object sender, RoutedEventArgs e)
    DependencyObject dep = sender as DependencyObject;
    while (!(dep is Button))
        dep = VisualTreeHelper.GetChild(dep, 0);

    Button button = dep as Button;

    object res = Resources["SelectAllButtonTemplate"];

    button.Template = res as ControlTemplate;

This method uses VisualTreeHelper to walk the visual tree. We are relying on the knowlegde that Button is the first element, four levels down the tree. If this were not the case, we would need some more sophisticated logic here.

Once the button is found, we cast then style. Here I am setting the control template because the existing template is not the template you would find on an average button.

Here is my template:

<ControlTemplate x:Key="SelectAllButtonTemplate" TargetType="{x:Type Button}">
        <TextBlock Text="Hi"/>

Colin E.
http://www.scottlogic.co.uk/blog/wpf/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples

Jan 21, 2009 at 9:58 PM

Vinsibal and Colin thank you very mach for your help.

Everything is working fine now.


Colin you wrote a great article http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx !

Vinsibal I am reading your demos and explanations for WPF DataGrid on your blog. Good work !!