问题
I need to create a style that rotates the x axis labels. I've found this examples: Easily rotate the axis labels of a Silverlight/WPF Toolkit chart and Rotating Text and How to change AxisLabelStyle in code behind?
I need to do this in the code behind. So I've tried this:
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;
I think I'm doing some mistake on the RenderTransformProperty.
What I have to do? Thanks!
Edit: O just updated my code to:
var labelStyle = new Style(typeof(AxisLabel));
var setter = new Setter(AxisLabel.RenderTransformProperty, new RotateTransform(){Angle = -60, CenterX = 40, CenterY = 30});
labelStyle.Setters.Add(setter);
And now it works fine!
回答1:
If you are doing this from code, you should focus on manipulating the transform collection.
var group = new TransformGroup();
group.Children.Add(new RotateTransform() { Angle = 270 });
control.RenderTransform = group;
Alternatively, add your transform to an existing 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;
}
oh... You should also think about your RenderTransformOrigin
:
control.RenderTransformOrigin = new Point(0.5, 0.5);
来源:https://stackoverflow.com/questions/6473360/create-style-to-rotate-axis-label-in-code-behind