silverlight – 创建样式以在后面的代码中旋转轴标签

我需要创建一个旋转x轴标签的样式.

我发现了这个例子:
Easily rotate the axis labels of a Silverlight/WPF Toolkit chart
Rotating Text
How to change AxisLabelStyle in code behind?

我需要在后面的代码中执行此操作.所以我试过这个:

var labelStyle = new Style(typeof(AxisLabel));
var rotate = new Setter(RotateTransform.AngleProperty, 270);
var setter = new Setter(AxisLabel.RenderTransformProperty, rotate);

labelStyle.Setters.Add(setter);
(xSerie.IndependentAxis as CategoryAxis).AxisLabelStyle = labelStyle;

我想我在RenderTransformProperty上做了一些错误.

我该怎么办?谢谢!

编辑:
我刚刚将我的代码更新为:

var labelStyle = new Style(typeof(AxisLabel));  
var setter = new Setter(AxisLabel.RenderTransformProperty, new RotateTransform(){Angle = -60, CenterX = 40, CenterY = 30});  
labelStyle.Setters.Add(setter);

现在它工作正常!

最佳答案 如果您是从代码执行此操作,则应专注于操作转换集合.

var group = new TransformGroup();
group.Children.Add(new RotateTransform() { Angle = 270 });
control.RenderTransform = group;

或者,将变换添加到现有的RenderTransform:

TransformGroup renderTransform = control.RenderTransform as TransformGroup;
if (renderTransform != null)
{
    RotateTransform rotate = renderTransform.Children
         .FirstOrDefault(o => o is RotateTransform as RotateTransform 
    if(rotate == null)
    {
         rotate = new RotateTransform();
         renderTransform.Children.Add(rotate);
    }
    rotate.Angle = 270;
}

哦……你还应该想想你的RenderTransformOrigin:

control.RenderTransformOrigin = new Point(0.5, 0.5);
点赞