如何通过保持自动调整大小功能在 WPF 中旋转文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6458086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:55:46  来源:igfitidea点击:

How to rotate Text in WPF by keeping the Auto-Sizing function

wpftextrotation

提问by Nasenbaer

I want to have an text vertical. I just use a simple grid in WPF to auto-size the areas. But when using RotateTransform, all calculations are wrong. Any idea how to solve this?

我想要一个垂直的文本。我只是在 WPF 中使用一个简单的网格来自动调整区域的大小。但是在使用的时候RotateTransform,所有的计算都是错误的。知道如何解决这个问题吗?

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

WPF rotate textIn this image you see what I mean. If I now want to auto-size the middle part I cannot use "Width" or "Height" property because both will raise a wrong sizing result. Width =120px will increase the horicontal (original) width and will make the complete row 120pixel. Height=120px will make the text 120pixel height.

WPF 旋转文本在这张图片中,你明白我的意思。如果我现在想自动调整中间部分的大小,我不能使用“宽度”或“高度”属性,因为两者都会产生错误的大小调整结果。Width =120px 将增加水平(原始)宽度并使整行为 120 像素。Height=120px 将使文本高度为 120 像素。

回答by Rachel

Use a LayoutTransforminstead of a RenderTransform. It gets applied during the layout pass, not during rendering.

使用 aLayoutTransform代替 a RenderTransform。它在布局过程中应用,而不是在渲染过程中应用。

回答by Max Mazur

Like Rachel said use LayoutTransform

就像雷切尔说的使用 LayoutTransform

<TextBlock Text="Goodday" >
   <TextBlock.LayoutTransform>
     <RotateTransform Angle="90" />
   </TextBlock.LayoutTransform>  
</TextBlock>

回答by mujtaba Hyder

<TextBlock Height="14" 
    x:Name="TextBlock1" 
    Text="Vertical Bottom to Up" Margin="73,0,115,0" RenderTransformOrigin="0.5,0.5" > 
    <TextBlock.RenderTransform> 
        <TransformGroup> 
            <ScaleTransform/> 
            <SkewTransform/> 
            <RotateTransform Angle="-90"/> 
            <TranslateTransform/> 
        </TransformGroup> 
    </TextBlock.RenderTransform> 
</TextBlock>