Silverlight: How to change AxisLabelStyle in code behind?
In the xaml file, we can change the AxisLabelStyle by doing this:
<chartingToolkit:ColumnSeries.IndependentAxis>
<chartingToolkit:CategoryAxis Orientation="X">
<chartingToolkit:CategoryAxis.AxisLabelStyle>
<Style TargetType="chartingToolkit:AxisLabel">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:AxisLabel">
<!--some code here-->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</chartingToolkit:CategoryAxis.AxisLabelSt开发者_开发百科yle>
</chartingToolkit:CategoryAxis>
</chartingToolkit:ColumnSeries.IndependentAxis>
My question is: how to add the AxisLabelStyle in code behind?
I know we can add DataPointStyle by doing this:
ColumnSeries CS = new ColumnSeries();
CS.DataPointStyle = Application.Current.Resources["ByteBlocksColumns"] as Style;
But apparently we cannot directly change the AxisLabelStyle like this because the AxisLabelStyle is inside a CategoryAxis.
Any one can help? Thanks!
I have changed your xaml
a little.
<charting:Chart>
<charting:ColumnSeries x:Name="CS" ItemsSource="{Binding Items}" IndependentValuePath="X" DependentValuePath="Y">
<charting:ColumnSeries.IndependentAxis>
<charting:CategoryAxis Orientation="X" />
</charting:ColumnSeries.IndependentAxis>
</charting:ColumnSeries>
</charting:Chart>
The xaml above can be written in c# so:
var CS = new ColumnSeries
{
ItemsSource = model.Items,
IndependentValuePath = "X",
DependentValuePath = "Y",
IndependentAxis = new CategoryAxis { Orientation = AxisOrientation.X }
};
And now in code-behind you can set the AxisLabelStyle
property in this way:
var labelStyle = new Style(typeof(AxisLabel));
labelStyle.Setters.Add(new Setter(AxisLabel.StringFormatProperty, "Category {0}"));
var axis = (CategoryAxis)CS.IndependentAxis;
axis.AxisLabelStyle = labelStyle;
Don't forget to cast the IndependentAxis
property to a correct type, because by default it has the IAxis
type which doesn't have a label style.
精彩评论