views:

37

answers:

1

How do you update a WPF chart when the collection changes? I am using WPF chart (System.Windows.Controls.DataVisualization.Toolkit.dllversion version 3.5.50211.1) to plot some simple data. The classes for the data look like this:

public class EngineMeasurementCollection : Collection<EngineMeasurement> 
{ 

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

In my Mainform, I am receiving new data on an event callback. This comes back on another thread. I look at the data to see if it has a new TimeStamp. If it is, I add it to my collection. I then dutifully call

Dispatcher.Invoke(myDelegate, DispatcherPriority.Normal,null) 

to get myself back on the GUI thread. I then call:

m_ctrlLineSeriesTorque.ItemsSource = m_TorqueCollection; 

to try and get the chart to update. I've verified I get into this code. I've also verified that I can display a chart if I just throw some values into the contructor of EngineMeasurementCollection. How do I get the chart to update as I add more values to the collection?

Somewhere I saw that there might be a "Refresh" method on the chart itself. I don't see that. Also, I saw that perhaps EngineMeasurementCollection should be an "Observable collection" and EngineMeasurement should implement some interface. True?

Thanks, Dave

+2  A: 

Yes, your EngineMeasurmentCollection should be an ObservableCollection. Then you won't have anything more to do but add (or remove) items to the collection and the WPF binding system will take care of the rest to update the chart. That's the reason you should use an observable collection.

Miky Dinescu
+1 As Miky D said. Make it a ObservableCollection. Or else implement INotifyProeprtyChanged in the class where EnginerMeasurementCollection is a Property and Raise the property changed notification.
Avatar
Thanks! Just what I needed.
Dave