I would like to enable text wrapping on all column headers of my DataGrid, without disabling the other default header functionality, such as column resizing, sort direction indicator, etc.
Is there a way to do this?
I would like to enable text wrapping on all column headers of my DataGrid, without disabling the other default header functionality, such as column resizing, sort direction indicator, etc.
Is there a way to do this?
I added


to the header text where I want the text to wrap. This is useful when you don't need to bind your header text to somethingInstead of assigning the column name directly to the DataGridColumn.Header property, I created a TextBlock containing the column name, set the TextWrapping property of the TextBlock to "Wrap" and assigned the TextBlock to the DataGridColumn.Header property. This preserves the default header functionality.
Example:
You need to add the following namespace to your app.xaml file:
and then add this tag to app.xaml:
This will add text-wrapping to the headers of all of your DataGrids throughout your WPF application.
While we're on the subject, if you just wanted to center the text of each of your DataGrid headers, you could do this using the following style instead:
Yet I've seen so many WPF articles suggesting that you can only do this by adding TextWrapping or HorizontalAlignment on each DataGrid column individually:
Or don't bother with the primitives in the app.xaml file and do the following (my objects):
You could create a global
Style
for your column headers. Without any example mark-up I don't know the syntax, but it should look something like this:Since the
Style
is key-less, it will automatically be applied to all of your column headers. And styles will not override any locally set properties, so it won't "disable" any existing header functionality.