Binding update on adds news series to WPF Toolkit chart (instead of replacing/updating series)
- by Mal Ross
I'm currently recoding a bar chart in my app to make use of the Chart class in the WPF Toolkit. Using MVVM, I'm binding the ItemsSource of a ColumnSeries in my chart to a property on my viewmodel. Here's the relevant XAML:
<charting:Chart>
<charting:ColumnSeries ItemsSource="{Binding ScoreDistribution.ClassScores}"
IndependentValuePath="ClassName" DependentValuePath="Score"/>
</charting:Chart>
And the property on the viewmodel:
// NB: viewmodel derived from Josh Smith's BindableObject
public class ExamResultsViewModel : BindableObject
{
// ...
private ScoreDistributionByClass _scoreDistribution;
public ScoreDistributionByClass ScoreDistribution
{
get
{
return _scoreDistribution;
}
set
{
if (_scoreDistribution == value)
{
return;
}
_scoreDistribution = value;
RaisePropertyChanged(() => ScoreDistribution);
}
}
However, when I update the ScoreDistribution property (by setting it to a new ScoreDistribution object), the chart gets an additional series (based on the new ScoreDistribution) as well as keeping the original series (based on the previous ScoreDistribution).
To illustrate this, here are a couple of screenshots showing the chart before an update (with a single data point in ScoreDistribution.ClassScores) and after it (now with 3 data points in ScoreDistribution.ClassScores):
Now, I realise there are other ways I could be doing this (e.g. changing the contents of the original ScoreDistribution object rather than replacing it entirely), but I don't understand why it's going wrong in its current form. Can anyone help?