Silverlight:如何在代码中改变AxisLabelStyle? [英] Silverlight: How to change AxisLabelStyle in code behind?

查看:257
本文介绍了Silverlight:如何在代码中改变AxisLabelStyle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xaml文件中,我们可以通过执行以下操作来更改AxisLabelStyle:

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.AxisLabelStyle>
    </chartingToolkit:CategoryAxis>
</chartingToolkit:ColumnSeries.IndependentAxis>

我的问题是如何在代码中添加AxisLabelStyle?

My question is: how to add the AxisLabelStyle in code behind?

我知道我们可以通过这样做添加DataPointStyle:

I know we can add DataPointStyle by doing this:

ColumnSeries CS = new ColumnSeries();
CS.DataPointStyle = Application.Current.Resources["ByteBlocksColumns"] as Style;

但是显然我们不能像这样直接改变AxisLabelStyle,因为AxisLabelStyle在CategoryAxis中。

But apparently we cannot directly change the AxisLabelStyle like this because the AxisLabelStyle is inside a CategoryAxis.

任何人都可以帮助?感谢!

Any one can help? Thanks!

推荐答案

我已经改变了你的 xaml p>

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>

上述xaml可以用c#写成:

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 }
         };   

现在在代码隐藏中你可以设置 AxisLabelStyle property以这种方式:

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;

不要忘记投放 IndependentAxis 属性为正确的类型,因为默认情况下它具有没有标签样式的 IAxis 类型。

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.

这篇关于Silverlight:如何在代码中改变AxisLabelStyle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆