2011-05-22 31 views
2

我有一個線型圖這樣創建:怎樣活在Silverlight更新圖表值4

<Grid.Resources> 
        <local:EngineMeasurementCollection x:Key="EngineMeasurementCollection"/> 


       </Grid.Resources> 



        <charting:Chart x:Name="ahorasi" Title="Engine Performance" Margin="-2,0,384,0"> 
        <!-- Power curve --> 
        <charting:LineSeries 
    Title="Power" 
    ItemsSource="{StaticResource EngineMeasurementCollection}" 
    IndependentValueBinding="{Binding Speed}" 
    DependentValueBinding="{Binding Power}" 

    > 
         <!-- Vertical axis for power curve --> 
         <charting:LineSeries.DependentRangeAxis> 
          <charting:LinearAxis 
      Orientation="Y" 
      Title="Power (hp)" 
      Minimum="0" 
      Maximum="250" 
      Interval="50" 
      ShowGridLines="True"/> 
         </charting:LineSeries.DependentRangeAxis> 
        </charting:LineSeries> 
        <!-- Torque curve --> 
        <charting:LineSeries 
    Title="Torque" 
    ItemsSource="{StaticResource EngineMeasurementCollection}" 
    IndependentValueBinding="{Binding Speed}" 
    DependentValueBinding="{Binding Torque}"> 
         <!-- Vertical axis for torque curve --> 
         <charting:LineSeries.DependentRangeAxis> 
          <charting:LinearAxis 
      Orientation="Y" 
      Title="Torque (lb-ft)" 
      Minimum="50" 
      Maximum="300" 
      Interval="50"/> 
         </charting:LineSeries.DependentRangeAxis> 
        </charting:LineSeries> 
        <charting:Chart.Axes> 
         <!-- Shared horizontal axis --> 
         <charting:LinearAxis 
     Orientation="X" 
     Title="Speed (rpm)" 
     Interval="1000" 
     ShowGridLines="True"/> 
        </charting:Chart.Axes> 
       </charting:Chart> 

然後,我有

 public class EngineMeasurementCollection : ObservableCollection<EngineMeasurement> 
{ 
    public EngineMeasurementCollection() 
    { 
     Add(new EngineMeasurement { Speed = 1000, Torque = 100, Power = 20 }); 
     Add(new EngineMeasurement { Speed = 2000, Torque = 160, Power = 60 }); 
     Add(new EngineMeasurement { Speed = 3000, Torque = 210, Power = 125 }); 
     Add(new EngineMeasurement { Speed = 4000, Torque = 220, Power = 160 }); 
     Add(new EngineMeasurement { Speed = 5000, Torque = 215, Power = 205 }); 
     Add(new EngineMeasurement { Speed = 6000, Torque = 200, Power = 225 }); 
     Add(new EngineMeasurement { Speed = 7000, Torque = 170, Power = 200}); 

    } 



} 

public class EngineMeasurement 
{ 
    public int Speed { get; set; } 
    public int Torque { get; set; } 
    public int Power { get; set; } 
}` 

當我跑我的項目的圖表可以完美運行,顯示值在構造函數中創建,但是這是硬編碼的,我找不到添加或刪除元素來更新圖表的方式。

我甚至不明白爲什麼圖表在構造函數中顯示提供的項目,是否創建了EngineMeasurementCollection的實例?自動?我不創建一個新的EngineMeasurementCollection。

我想是這樣的:

EngineMeasurement littleitem = new EngineMeasurement(); 
EngineMeasurementCollection fullitems = new EngineMeasurementCollection(); 
littleitem.Power = 10; 
littleitem.Speed = 1000; 
littleitem.Torque = 50; 
fullitems.Add(littleitem); 
//Up to this point everything works perfect, now I want to update my chart with fullitem values in whatever possible way. 

我不能做ahorasi.ItemsSource = fullitems因爲.ItemsSource不存在,我真的很困惑,因爲,剛剛創建當圖表精彩的作品,用硬編碼值,我認爲更新它必須只是一個步驟,但無法找到如何。

回答

0

這條線在XAML中: -

   <local:EngineMeasurementCollection x:Key="EngineMeasurementCollection"/> 

正在創建一個實例到EngineMeasurementCollection及其這種情況下您的圖表綁定到。

所以如果你想添加新的元素到你的圖表中,你需要修改這個實例。假設Grid認爲,這是一種資源的名稱爲「LayoutRoot」您可以通過添加這個屬性給你UserControl訪問集合: - :

-

public EngineMeasurementCollection CurrentMeasurements 
{ 
     get { return (EngineMeasurementCollection)LayoutRoot.Resources["EngineMeasurementCollection"]; } 
} 

現在,添加一個額外的與入境

CurrentMeasurements.Add(new EngineMeasurement() { Power = 10, Speed = 1000, Torque = 50 }); 
+0

非常感謝你安東尼,現在看起來很簡單,謝謝你的幫助,非常感謝。 – nxgtrturbo 2011-05-23 19:52:45

0

我的具體問題已經解決,但獲得進一步的讀者,我的用戶頁面是一個比我用圖表不同的頁面,所以我加了

public EngineMeasurementCollection CurrentMeasurements 

到我的圖表頁面,它的工作原理非常完美,不知道它是否包含代碼。