我有一個線型圖這樣創建:怎樣活在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不存在,我真的很困惑,因爲,剛剛創建當圖表精彩的作品,用硬編碼值,我認爲更新它必須只是一個步驟,但無法找到如何。
非常感謝你安東尼,現在看起來很簡單,謝謝你的幫助,非常感謝。 – nxgtrturbo 2011-05-23 19:52:45