2011-03-16 20 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

相關問題