文字显示在一个 Canvas 中(定位用Canvas.SetLeft() / Canvas.SetTop() ),
为了实现排版效果,可适当在 TextBlock 外套一层 StackPanel 或 DockPanel
DockPanel pnl = new DockPanel();
TextBlock titleBlock = new TextBlock();
titleBlock.LayoutTransform = new RotateTransform()
{
Angle = 270 // or 90
};
titleBlock.VerticalAlignment = VerticalAlignment.Center;
titleBlock.Text = "Here some Text";
Canvas.SetLeft(pnl, 50);
Canvas.SetTop(pnl, 100);
这时候,文本旋转,跟定位时的 坐标 X Y 并没有相互影响(也就是上面的设位置,不需要作什么特别考虑)。
而另一种情况:
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
// ...
// DrawTitle
var ft = new FormattedText("Some Text Here", CultureInfo.CurrentCulture,
FlowDirection.LeftToRight, new Typeface("Tahoma"), 15, Brushes.Black);
// use ft.Width and ft.Height to calculate POSITIONS
RotateTransform RT = new RotateTransform() { Angle = 270 }; // 要旋转的角度
var point = new Point((500 - ft.Height) / (-2), (600 - ft.Width)/2); // 注意这里:因为旋转使横纵对调,所以这里的 Point(X,Y) 也就变为 原参照系的 (Y,X)值
dc.PushTransform(RT);
dc.DrawText(ft, point );
dc.Pop();
// Do other Drawing..
}
原文:https://www.cnblogs.com/CoderMonkie/p/rotate-text-in-wpf.html