如何在数据网格中重新创建此标头

时间:2013-06-04 03:58:15

标签: c# wpf xaml

如下图所示,我在第5列中有一个带有旋转文本的列标题。我只关心那一个。

enter image description here

到目前为止XAML是这样的:

<UserControl.Resources>
    <Style x:Key="rotatedTextStart" TargetType="TextBlock">
        <Setter Property="LayoutTransform">
            <Setter.Value><RotateTransform Angle="-45" /></Setter.Value>
        </Setter>
        <Setter Property="VerticalAlignment" Value="Bottom"/>
        <Setter Property="Width" Value="130"/>
        <Setter Property="Margin" Value="12,0,0,0"/>
    </Style>
    <Style x:Key="rotatedTextMiddle" TargetType="TextBlock">
        <Setter Property="LayoutTransform">
            <Setter.Value><RotateTransform Angle="-45" /></Setter.Value>
        </Setter>
        <Setter Property="VerticalAlignment" Value="Bottom"/>
        <Setter Property="Width" Value="130"/>
        <Setter Property="Margin" Value="-50,0,0,0"/>
    </Style>
    <Style x:Key="rotatedTextEnd" TargetType="TextBlock">
        <Setter Property="LayoutTransform">
            <Setter.Value><RotateTransform Angle="-45" /></Setter.Value>
        </Setter>
        <Setter Property="VerticalAlignment" Value="Bottom"/>
        <Setter Property="Width" Value="130"/>
        <Setter Property="Margin" Value="-50,0,12,0"/>
    </Style>
</UserControl.Resources>

<DataGrid AutoGenerateColumns="False" AlternatingRowBackground="{x:Null}" DataContext="{StaticResource UserGroups}" ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Name}"/>
        <DataGridTextColumn Header="Job Title" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Job_Title}"/>
        <DataGridTextColumn Header="Department" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Department}"/>
        <DataGridTextColumn Header="Company" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Company}"/>

        <DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=Company}" CanUserResize="False" Width="580">
            <DataGridTextColumn.Header>
                <StackPanel Orientation="Vertical">
                    <TextBlock HorizontalAlignment="Center">Modules</TextBlock>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock  Style="{StaticResource rotatedTextStart}" >Customer Services</TextBlock>
                        <TextBlock Style="{StaticResource rotatedTextMiddle}" >Asset Management</TextBlock>
                        <TextBlock Style="{StaticResource rotatedTextMiddle}" >Works Management</TextBlock>
                        <TextBlock Style="{StaticResource rotatedTextMiddle}" >Project Management</TextBlock>
                        <TextBlock Style="{StaticResource rotatedTextMiddle}" >Rates Management</TextBlock>
                        <TextBlock Style="{StaticResource rotatedTextMiddle}" >Finance</TextBlock>
                        <TextBlock Style="{StaticResource rotatedTextMiddle}" >Human Resources</TextBlock>
                        <TextBlock Style="{StaticResource rotatedTextMiddle}" >Document Management</TextBlock>
                        <TextBlock Style="{StaticResource rotatedTextMiddle}" >User Management</TextBlock>
                        <TextBlock Style="{StaticResource rotatedTextEnd}" >Configuration</TextBlock>
                    </StackPanel>
                </StackPanel>
            </DataGridTextColumn.Header>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

我有一个List&lt; module&gt;名为_modules的模块具有以下属性:

public class Module
{
    public string ModuleName { get; set; }
    public string ModuleAbbreviation { get; set; }
    public string ModuleColor { get; set; }
    public bool ModuleAvailable { get; set; }
}

例如:ModuleName =“客户服务”

我想弄清楚的是如何通过将_modules List绑定到某种类型的控件来创建相同的旋转头。我应该使用什么控制?

[编辑]转移到列表视图并使用文本块注释掉堆栈面

<DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=Company}" CanUserResize="False" Width="580">
    <DataGridTextColumn.Header>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
            <TextBlock HorizontalAlignment="Center">Modules</TextBlock>
            <!--
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Style="{StaticResource rotatedTextStart}" >Customer Services</TextBlock>
                                <TextBlock Style="{StaticResource rotatedTextMiddle}" >Asset Management</TextBlock>
                                <TextBlock Style="{StaticResource rotatedTextMiddle}" >Works Management</TextBlock>
                                <TextBlock Style="{StaticResource rotatedTextMiddle}" >Project Management</TextBlock>
                                <TextBlock Style="{StaticResource rotatedTextMiddle}" >Rates Management</TextBlock>
                                <TextBlock Style="{StaticResource rotatedTextMiddle}" >Finance</TextBlock>
                                <TextBlock Style="{StaticResource rotatedTextMiddle}" >Human Resources</TextBlock>
                                <TextBlock Style="{StaticResource rotatedTextMiddle}" >Document Management</TextBlock>
                                <TextBlock Style="{StaticResource rotatedTextMiddle}" >User Management</TextBlock>
                                <TextBlock Style="{StaticResource rotatedTextEnd}" >Configuration</TextBlock>
                            </StackPanel>
                            -->
            <ListView Width="190">
                <ListViewItem>kjh</ListViewItem>
                <ListViewItem>kjh</ListViewItem>
            </ListView>
        </StackPanel>
    </DataGridTextColumn.Header>
</DataGridTextColumn>

enter image description here

1 个答案:

答案 0 :(得分:2)

_modules绑定到GridViewListView。使用TextBlock的{​​{1}}样式应用于列标题

rotatedTextMiddle