tags:

views:

2317

answers:

3

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?

A: 

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:

<Style TargetType="{x:Type dg:ColumnHeader}">
 <Setter Property="TextWrapping" Value="Wrap"/>
</Style>

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.

Charlie
Thanks for your suggestion, but I have not been able to get this approach to work. However I did find that I can get my desired result if instead of assigning a string to the DataGridColumn.Header property, I create a TextBlock containing my string and set the TextWrapping property of the text block to "Wrap".
what about wrapping the content no header?
Nitin Chaudhari
+3  A: 

I figured out one solution. Instead 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.

Peter

+4  A: 

I would like to enable text wrapping on all column headers of my DataGrid, without disabling the other default header functionality

You need to add the following namespace to your app.xaml file:

xmlns:primitives="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"

and then add this tag to app.xaml:

<Style TargetType="{x:Type primitives:DataGridColumnHeader}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

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:

<Style TargetType="{x:Type primitives:DataGridColumnHeader}">
    <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>

Simple, hey ? 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:

<toolkit:DataGridTextColumn Binding="{Binding Path=TaxAmount}">
    <toolkit:DataGridTextColumn.HeaderTemplate >
        <DataTemplate>
            <TextBlock Text="Tax Amount" Width="90" TextWrapping="Wrap" HorizontalAlignment="Center"/>
        </DataTemplate>
    </toolkit:DataGridTextColumn.HeaderTemplate>
</toolkit:DataGridTextColumn>

Hope this saves you a bit of Googling !

Mike Gledhill
Wow, you gave a good answer to a year-old question an hour before I looked for it. +1
Quartermeister
@Mike I do not know why, but I tried all your code but It did not center the text?
Lisa