Click here to Skip to main content
15,887,416 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys
Please kindly see the picture Intersection — Freeimage.host[^]
As you see there is a white space between RowHeaders and ColumnHeaders in DataGrid. So I need to know how may I change its color?
Thanks.

What I have tried:

I tried to changed the background of RowHeader, also ColumnHeader, but the color of it, did not changed.
I searched internet but I could not find any code for it.
Posted

1 solution

Best place to understand a WPF control is to look at the Default Template. For the DataGrid it can be found here: DataGrid Styles and Templates - WPF .NET Framework | Microsoft Learn[^].

What you are pointing to is the Select All button in the top-left corner of the DataGrid.

Using the link above, that is the DataGridSelectAllButtonStyle. It is the very first style found on the page linked above.
XML
<Style TargetType="{x:Type Button}"
       x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, 
       TypeInTargetAssembly={x:Type DataGrid}}">
    <!-- trimmed -->
</Style>

Do a search of the template and you can see where it is applied. This is the style that you need to modify.
XML
<ControlTemplate TargetType="{x:Type DataGrid}">
    <!-- trimmed -->
		<Button Focusable="false"
			Command="{x:Static DataGrid.SelectAllCommand}"
			Style="{DynamicResource {ComponentResourceKey 
			ResourceId=DataGridSelectAllButtonStyle, 
			TypeInTargetAssembly={x:Type DataGrid}}}"
			Visibility="{Binding HeadersVisibility, 
			ConverterParameter={x:Static DataGridHeadersVisibility.All}, 
			Converter={x:Static DataGrid.HeadersVisibilityConverter}, 
			RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
			Width="{Binding CellsPanelHorizontalOffset, 
			RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
    <!-- trimmed -->
</ControlTemplate>

So you need to modify the DataGridSelectAllButtonStyle and apply a new ControlTemplate. There is a link on that page that will direct you on how to create a ControlTemplate and a link to a sample at the bottom of the page.
 
Share this answer
 
Comments
Sh.H. 15-Jan-24 4:57am    
@Graeme_Grant
Thanks. Worked!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900