旋转datagrid列标题中的文本

时间:2018-08-07 09:28:59

标签: c# wpf datagrid

因此,我正在尝试旋转数据网格中的文本,以免占用过多水平空间,而将其垂直放置。我应该怎么做?我有以下代码:

<DataGrid x:Name="list_results" IsReadOnly="True" Grid.Row="1" ColumnHeaderStyle="{StaticResource HeaderStyle}" ColumnWidth="Auto" RowBackground="Transparent" Foreground="#fafafa" AlternatingRowBackground="#555a62" Background="Transparent" BorderThickness="0" AutoGenerateColumns="False" RowHeaderWidth="0" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="pos" Header="Pos" />
        <DataGridTextColumn x:Name="Nr" Header="Nr" Binding="{Binding team_number}" />
        <DataGridTextColumn x:Name="pilot" Header="Naam piloot" Binding="{Binding team_data.pilot.firstname}" />
        <DataGridTextColumn x:Name="pilot_club" Header="Club" Binding="{Binding team_data.pilot.club}" />
        <DataGridTextColumn x:Name="pilot_license" Header="Licentie" Binding="{Binding team_data.pilot.license}" />
        <DataGridTextColumn x:Name="navigator" Header="Naam copiloot" Binding="{Binding team_data.navigator.firstname}" />
        <DataGridTextColumn x:Name="navigator_club" Header="Club" Binding="{Binding team_data.navigator.club}" />
        <DataGridTextColumn x:Name="navigator_license" Header="Licentie" Binding="{Binding team_data.navigator.license}" />
        <DataGridTextColumn x:Name="part_1_tot_gtk" Header="Totaal GTK's" Binding="{Binding team_data.navigator.license}" />
    </DataGrid.Columns>
</DataGrid>

,我想将part_1_tot_gtk的形式从horizontal旋转到vertical。将会给出以下内容;

datagrid

1 个答案:

答案 0 :(得分:2)

您可以在Window的资源中创建特定样式:

<Style x:Key="ColumnHeaderRotateStyle" TargetType="DataGridColumnHeader">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock TextWrapping="Wrap" Text="{Binding}">
                    <TextBlock.LayoutTransform>
                        <RotateTransform Angle="270" />
                    </TextBlock.LayoutTransform>
                </TextBlock>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>

然后将其与要旋转的列一起使用:

<DataGrid AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Column 1" Width="*" HeaderStyle="{StaticResource ColumnHeaderRotateStyle}" />
        <DataGridTextColumn Header="Column 2" Width="*" />
        <DataGridTextColumn Header="Column 3" Width="*" />
    </DataGrid.Columns>
</DataGrid>

我希望它能为您提供帮助