2011-03-16 44 views
0

在XAML文件中,我们可以通过这样改变AxisLabelStyle:Silverlight:如何在代码后面更改AxisLabelStyle?

<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后面?

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

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

但很显然,我们不能直接改变这样的AxisLabelStyle因为AxisLabelStyle是则CategoryAxis内。

任何人都可以提供帮助吗?谢谢!

+0

但你可以使用类似这样的轴:_chart.ColumnSeries [0] .IndependentAxis.AxisLabelStyle = ...;我没有测试过这段代码,所以我不知道这段代码是否可以解决你的问题。 – vorrtex 2011-03-16 18:59:02

+0

感谢您的评论。我之前尝试过,但在IndependentAxis中我找不到AxisLabelStyle。 – Mrainy 2011-03-20 19:34:20

+0

我已经发布了适用于我的应用程序的答案。 – vorrtex 2011-03-20 22:20:52

回答

1

我改变了你的xaml一点。

<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#这样:

var CS = new ColumnSeries 
     { 
      ItemsSource = model.Items, 
      IndependentValuePath = "X", 
      DependentValuePath = "Y", 
      IndependentAxis = new CategoryAxis { Orientation = AxisOrientation.X } 
     }; 

现在在后台代码,你可以设置这样的AxisLabelStyle属性:

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类型不具有标签样式。

+0

嗨,Vorrtex。在我的应用程序中,我在运行时添加了MyColumnSeries。所以当我无法获取轴=(CategoryAxis)MyColumnSeries.IndependentAxis。当我尝试设置轴的AxisLabelStyle时,它抛出一个null引用异常。 – Mrainy 2011-03-22 14:58:48

+0

@Mrainy创建ColumnSeries时,请在下一行设置IndependentAxis属性。 CS.IndependentAxis = new CategoryAxis {Orientation = AxisOrientation.X}; – vorrtex 2011-03-22 16:42:27

+0

@Mrainy我编辑了我的答案,检查第二和第三块代码。 – vorrtex 2011-03-22 18:42:02

相关问题