xx.xmal.cs 后台代码中动态添加控件到 UI
文字显示在一个 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 并没有相互影响(也就是上面的设位置,不需要作什么特别考虑)。
而另一种情况:
在渲染过程中自定义渲染内容 ( 重写的 OnRender )
// Sample -- Ctrl => Height:500 Width:600
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
// TODO : for sample
var CtrlWidth = 600;
var CtrlHeight = 400;
// ...
// 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((CtrlHeight - ft.Width) / 2 - CtrlWidth, (CtrlWidth - ft.Height)/2 - CtrlHeight); // 注意这里的计算 很容易迷糊
dc.PushTransform(RT);
dc.DrawText(ft, point );
dc.Pop();
// Do other Drawing..
}