Rotates a WPF DataGrid at design-time and run-time, including row-headers and row-details, using only XAML.

Also clears up text "smudging" that usually happens with rotating text.

Image of Rotated WPF DataGrid

RotatedDataGrid.jpg

How to Rotate a WPF DataGrid

Add the following sections to your DataGrid:
<DataGrid.Resources>
    <Style x:Key="DataGridBase" TargetType="Control">
        <Setter Property="LayoutTransform">
            <Setter.Value>
                <TransformGroup>
                    <RotateTransform Angle="-90" />
                    <ScaleTransform ScaleX="1" ScaleY="-1" />
                </TransformGroup>
            </Setter.Value>
        </Setter> 
        <Setter Property="TextOptions.TextFormattingMode" Value="Display" />
    </Style >
    <Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridBase}"/>
    <Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource DataGridBase}"/>
    <Style TargetType="DataGridRowHeader" BasedOn="{StaticResource DataGridBase}"/>
</DataGrid.Resources>             
           
<DataGrid.LayoutTransform>
    <TransformGroup>
        <RotateTransform Angle="90" />
        <MatrixTransform Matrix="-1, 0, 0, 1, 0, 0" />
    </TransformGroup>
</DataGrid.LayoutTransform>


To rotate Row Details, apply the same LayoutTransform as the DataGrid:
<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <StackPanel>
            <StackPanel.LayoutTransform>
                <TransformGroup>
                    <RotateTransform Angle="90" />
                    <MatrixTransform Matrix="-1, 0, 0, 1, 0, 0" />
                </TransformGroup>
            </StackPanel.LayoutTransform>
            <TextBlock Height="100" Text="Binding Image" />
        </StackPanel>
    </DataTemplate>
</DataGrid.RowDetailsTemplate>

Note: Some CellStyle choices will prevent the DataGrid from rotating completely, so if your DataGrid does not display as illustrated above, please remove the misbehaving CellStyle settings.

Last edited Oct 12, 2012 at 1:25 AM by onejbsmith, version 12