在WPF TextBox中旋转文本而不旋转控件本身

时间:2017-04-12 04:25:38

标签: c# wpf

问题是:如何在没有旋转TextBox本身的情况下旋转WPF TextBox 中的文本?的 (Rotate text only) 我尝试了不同的RenderTransforms,LayoutTransforms,但这对我没有帮助。

2 个答案:

答案 0 :(得分:1)

找到答案我自己:将TextBox放在其他控件中,即在RIchTextBox中:

  <RichTextBox>
    <FlowDocument>
        <Paragraph>
          <TextBlock Text="Stacked text!">
          <TextBlock.LayoutTransform>
            <RotateTransform Angle="-90"></RotateTransform>
          </TextBlock.LayoutTransform>
          </TextBlock>
        </Paragraph>
      </FlowDocument>
  </RichTextBox>

答案 1 :(得分:1)

我想说问题的正确解决方案是为ControlTemplate定义自定义TextBox并将RotateTransform应用于ScrollViewer元素:

<TextBox Background="Yellow" Text="test">
    <TextBox.Template>
        <ControlTemplate TargetType="{x:Type TextBox}">
            <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
                    <ScrollViewer.LayoutTransform>
                        <RotateTransform Angle="-90"></RotateTransform>
                    </ScrollViewer.LayoutTransform>
                </ScrollViewer>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="true">
                    <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </TextBox.Template>
</TextBox>