Search code examples
wpfchartswpftoolkit

How do I update my chart to make it display recently added points


I want to add points to my linechart but it does not change visually. The first time I click the button it adds two points. Every other click does nothing, why not?

C# Code:

   List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
    private void Button_Click_3(object sender, System.Windows.RoutedEventArgs e)
    {

        valueList.Add(new KeyValuePair<string, int>(DateTime.Now.ToString("T"), 130));
        System.Threading.Thread.Sleep(1000);
        valueList.Add(new KeyValuePair<string, int>(DateTime.Now.ToString("T"), 140));

       lineSeries1.ItemsSource = valueList;


    }

XAML:

<chartingToolkit:Chart Canvas.Top="80" Canvas.Left="10" Name="mcChart" Width="400" Height="250" Background="LightSteelBlue">
    <chartingToolkit:Chart.Series>
            <chartingToolkit:LineSeries x:Name="lineSeries1" Title="Test" IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value}">
            </chartingToolkit:LineSeries>
    </chartingToolkit:Chart.Series>
</chartingToolkit:Chart>

Here is a picture of the result: As said above the second click on the same button doesn't do anything. Chart


Solution

  • Use an ObservableCollection instead of a List.

    ObservableCollection<KeyValuePair<string, int>> valueList = 
        new ObservableCollection<KeyValuePair<string, int>>();