C# 在WPF中相对于其容器绘制对角线文本/文本块/标签/控件

C# 在WPF中相对于其容器绘制对角线文本/文本块/标签/控件,c#,wpf,xaml,expression-blend,blend,C#,Wpf,Xaml,Expression Blend,Blend,我有一个动态大小的网格。我想在里面画一个对角线TextBlock。我已经有了一个对角线路径,您可以在其中设置线几何体进行动态调整。但是我在文本块中找不到挂件。我错过什么了吗 <Grid> <TextBlock Text="Draft" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="180" FontWeight="Bold" Foreground="#FFA43A3A" RenderT

我有一个动态大小的
网格
。我想在里面画一个对角线
TextBlock
。我已经有了一个对角线
路径
,您可以在其中设置
线几何体
进行动态调整。但是我在
文本块中找不到挂件。我错过什么了吗

<Grid>
    <TextBlock Text="Draft" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="180" FontWeight="Bold" Foreground="#FFA43A3A" RenderTransformOrigin="0.5,0.5"/>
    <Path Grid.Row="2" Grid.Column="0" Stroke="Red" StrokeThickness="2" Stretch="Fill">
        <Path.Data>
            <LineGeometry StartPoint="1,0" EndPoint="0,1" />                
        </Path.Data>            
    </Path>
</Grid>

预览

目标 这是我想要的,而无需设置绝对
RotateTransform

两种解决方案的比较
red
前景是FrankM解决方案,
green
一个来自Henrik Hansen


每当网格大小发生变化时,您必须计算文本块的角度,例如使用转换器

以下是一个例子:

<Window x:Class="WpfApplication13.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:WpfApplication13"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:AngleConverter x:Key="AngleConverter"/>
    </Window.Resources>
    <Grid x:Name="grid">
        <TextBlock Text="Draft" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="120" FontWeight="Bold" Foreground="#FFA43A3A"
                   RenderTransformOrigin="0.5,0.5">
            <TextBlock.RenderTransform>
                <MultiBinding Converter="{StaticResource AngleConverter}">
                    <MultiBinding.Bindings>
                        <Binding ElementName="grid" Path="ActualWidth"/>
                        <Binding ElementName="grid" Path="ActualHeight"/>
                    </MultiBinding.Bindings>
                </MultiBinding>
            </TextBlock.RenderTransform>
        </TextBlock>
        <Path Stroke="Red" StrokeThickness="2" Stretch="Fill">
            <Path.Data>
                <LineGeometry StartPoint="1,0" EndPoint="0,1" />
            </Path.Data>
        </Path>
    </Grid>
</Window>
结果:


您也可以将文本块包装在一个视图框中。它也会缩放文本,但这可能是不可取的:

<Viewbox Stretch="Fill" StretchDirection="Both">
  <TextBlock Text="Draft" FontWeight="Bold" Foreground="#FFA43A3A" Margin="5" RenderTransformOrigin="0.5,0.5" >
    <TextBlock.RenderTransform>
      <RotateTransform Angle="-35.0" />
    </TextBlock.RenderTransform>
  </TextBlock>
</Viewbox>



我以为您不需要额外的
转换器。无论如何谢谢你@Dominicjons这可以通过代码隐藏以一种非常类似的方式实现,但我认为这个解决方案在WPF中是非常充分和惯用的。应该自动计算
角度,如果父对象的大小发生变化,这将不起作用。@Dominicjons:当我的机器上的大小发生变化时,它可以正常工作,视图框调整角度-这就是添加视图的目的:-)我刚刚测试了它,发现虽然透视图被额外扭曲了!如果你想要这种效果,它肯定是一个解决方案!请参阅我的原始帖子以获得直接的比较。
<Viewbox Stretch="Fill" StretchDirection="Both">
  <TextBlock Text="Draft" FontWeight="Bold" Foreground="#FFA43A3A" Margin="5" RenderTransformOrigin="0.5,0.5" >
    <TextBlock.RenderTransform>
      <RotateTransform Angle="-35.0" />
    </TextBlock.RenderTransform>
  </TextBlock>
</Viewbox>