Binding update on adds news series to WPF Toolkit chart (instead of replacing/updating series)

Posted by Mal Ross on Stack Overflow See other posts from Stack Overflow or by Mal Ross
Published on 2010-06-14T16:50:38Z Indexed on 2010/06/14 16:52 UTC
Read the original article Hit count: 250

Filed under:
|
|
|

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):

With a single data point

With 2 more data points added - note the original wide bar behind them

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?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about binding