Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable text wrapping on all column headers?

People also ask

How do you wrap text in column headings?

I can wrap the text in the column headings, so the focus is on the contents in the cell, not on the width of the column. I select the entire row A1, and right click. I then select format cells, and click Wrap Text. Under Text alignment, select the Vertical text box and select Top.

How do I wrap column headings in Excel?

On the Home tab, in the Alignment group, click Wrap Text. (On Excel for desktop, you can also select the cell, and then press Alt + H + W.) Notes: Data in the cell wraps to fit the column width, so if you change the column width, data wrapping adjusts automatically.

How do you enable text wrapping?

Enable or disable text wrapping for a text box, rich text box, or expression box. Right-click the control for which you want to enable or disable text wrapping, and then click Control Properties on the shortcut menu. Click the Display tab. Select or clear the Wrap text check box.

How do I change text wrapping settings?

Click the Table Layout tab, and then under Settings, click Properties. Under Text Wrapping, click Around. To set the horizontal and vertical position of the table, the distance from surrounding text, and other options, under Text Wrapping, click Positioning, and then choose the options that you want.


Or don't bother with the primitives in the app.xaml file and do the following (my objects):

<DataGrid Name="WBdataGrid" AutoGenerateColumns="False" ColumnHeaderHeight="50" >
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.ColumnHeaderStyle>
    <DataGrid.Columns> ...

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>

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>

I added &#x0a; 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 something

<DataGridTextColumn x:Name="cAccountNumber" 
                    Header="Account&#x0a; Number" 
                    Width="80" 
                    Binding="{Binding Path=AccountNumber}" />

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.

Example:

<toolkit:DataGridTextColumn Binding="{Binding Path=MyProperty}">
    <toolkit:DataGridTextColumn.Header>
        <TextBlock Text="Something Longer" TextWrapping="Wrap" />
    </toolkit:DataGridTextColumn.Header>
</toolkit:DataGridTextColumn>

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.