如下图所示,我在第 5 列有一个带有旋转文本的列标题。我只关心那个。
到目前为止的 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>
我有一个名为 _modules 的 List<module>,模块具有以下属性:
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 列表绑定到某种类型的控件来创建相同的旋转标题。我应该使用什么控件?
[编辑] 转移到列表视图并用文本块注释掉堆栈面板后
<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>