如何更新我的图表以使其显示最近添加的点

时间:2017-04-18 19:47:56

标签: wpf charts wpftoolkit

我想在我的线图中添加点数,但它不会在视觉上改变。我第一次点击按钮就会增加两点。每次其他点击什么都不做,为什么不呢?

C#代码:

   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>

以下是结果的图片:如上所述,第二次点击同一按钮不会做任何事情。 Chart

1 个答案:

答案 0 :(得分:0)

使用ObservableCollection而不是List。

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