Я работаю над выпуском своей первой утилиты, но у меня возникают проблемы с выводом заголовка сетки данных так, как я хочу. Я пытаюсь повернуть заголовок сетки данных на 45 градусов, чтобы увидеть всю сетку данных в окне. В настоящее время моя сетка данных слишком велика, потому что заголовок не вращается.
Вот простой поворот, но повернут только текст:
Начальный 45-градусный тест
![enter image description here](https://i.stack.imgur.com/lbXPi.png)
В моем app.xaml у меня есть два других стиля, которые я уже изменил, чтобы приблизиться (в настоящее время закомментирован).
Второй пробный заголовок очень близок, но заголовок урезан:
Test2 - Это желаемое расположение, за исключением моего текста и обрезания "строк".
Я побежал третий тест, в котором заголовок находится близко, но заголовок расположен неправильно.
Test3 - заголовок неправильно расположен
![enter image description here](https://i.stack.imgur.com/fxO5T.png)
Я создал очень простой проект для тестирования.
В MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DatagridHeaderProject
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<User> _UserOC;
public ObservableCollection<User> UserOC
{
get { return _UserOC; }
set { _UserOC = value; }
}
public MainWindow()
{
InitializeComponent();
User User1 = new User(1, "Dave", true, false, true);
User User2 = new User(2, "Pete", false, true, true);
User User3 = new User(3, "Mary", false, false, false);
User User4 = new User(4, "Jane", false, false, true);
UserOC = new ObservableCollection<User>();
UserOC.Add(User1);
UserOC.Add(User2);
UserOC.Add(User3);
UserOC.Add(User4);
}
}
public class User
{
public User(int id, string name, bool cb1, bool cb2, bool cb3)
{
ID = id;
Name = name;
CheckBox1 = cb1;
CheckBox2 = cb2;
CheckBox3 = cb3;
}
private int _ID;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private bool _CheckBox1;
public bool CheckBox1
{
get { return _CheckBox1; }
set { _CheckBox1 = value; }
}
private bool _CheckBox2;
public bool CheckBox2
{
get { return _CheckBox2; }
set { _CheckBox2 = value; }
}
private bool _CheckBox3;
public bool CheckBox3
{
get { return _CheckBox3; }
set { _CheckBox3 = value; }
}
}
}
В моем MainWindow.XAML
<Window x:Class="DatagridHeaderProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DatagridHeaderProject"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid ItemsSource="{Binding UserOC, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}" Style="{StaticResource DG_Style}" IsReadOnly="True" SelectionMode="Single" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ID}" Header="ID" />
<DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<DataGridCheckBoxColumn Binding="{Binding CheckBox1}" Header="Check Box 1 header is longer"/>
<DataGridCheckBoxColumn Binding="{Binding CheckBox2}" Header="Check Box 2"/>
<DataGridCheckBoxColumn Binding="{Binding CheckBox3}" Header="check Box 3"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
В моем App.xaml:
<Application x:Class="DatagridHeaderProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DatagridHeaderProject"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="DG_Style" TargetType="DataGrid" >
<Setter Property="Margin" Value="10" />
<Setter Property="Height" Value="500" />
<Setter Property="Width" Value="Auto" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Grid.RowSpan" Value="2" />
<Setter Property="Grid.ColumnSpan" Value="1" />
<Setter Property="AutoGenerateColumns" Value="False" />
<Setter Property="AlternatingRowBackground" Value="LightBlue" />
<Setter Property="CanUserAddRows" Value="False" />
<Setter Property="CanUserDeleteRows" Value="False" />
<Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
<Setter Property="ColumnHeaderStyle" Value="{DynamicResource RotatedHeader}"/>
</Style>
<Style x:Key="RotatedHeader" TargetType="DataGridColumnHeader" >
<Setter Property="ContentTemplate" >
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding}">
<TextBlock.LayoutTransform>
<RotateTransform Angle="315" />
</TextBlock.LayoutTransform>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<!--This style is very close to what is needed, but the grid width cuts off the text-->
<!--<Style x:Key="RotatedHeader" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<DockPanel Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}" MinWidth="30" Background="AliceBlue">
<Rectangle Name="HeaderRect" Stroke="Black" >
<Rectangle.RenderTransform>
<SkewTransform CenterX="0" AngleX="-60" AngleY="0" CenterY="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualHeight }"/>
</Rectangle.RenderTransform>
</Rectangle>
<TextBlock Text="{TemplateBinding Content}" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="80">
<TextBlock.LayoutTransform>
<RotateTransform Angle="-30"/>
</TextBlock.LayoutTransform>
<TextBlock.RenderTransform>
<TranslateTransform X="20"/>
</TextBlock.RenderTransform>
</TextBlock>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>-->
<!--This is close, but the header container is too small so the text gets cut off.-->
<!--<Style x:Key="RotatedHeader" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<StackPanel Height="50">
<Grid ShowGridLines="True" Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}" MinWidth="30" Height ="Auto" Background="AliceBlue" RenderTransformOrigin="0.5,0.5" >
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform AngleX="60"/>
<RotateTransform Angle="-30"/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<Rectangle Name="HeaderRect" Stroke="Black" >
<Rectangle.RenderTransform>
<SkewTransform CenterX="0" AngleX="0" AngleY="0" CenterY="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualHeight }"/>
</Rectangle.RenderTransform>
</Rectangle>
<TextBlock Text="{TemplateBinding Content}" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="80">
<TextBlock.LayoutTransform>
<RotateTransform Angle="0"/>
</TextBlock.LayoutTransform>
<TextBlock.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform AngleX="-60"/>
<RotateTransform/>
<TranslateTransform X="20"/>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>
</Grid>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>-->
</Application.Resources>
Любая помощь, которую вы можете предложить, приветствуется. Я все еще новичок в этом, так как это мой первый настоящий проект.
Спасибо,
Дэйв