C# 如何实时刷新WPF Toolkit ColumnSeries图表

C# 如何实时刷新WPF Toolkit ColumnSeries图表,c#,wpf,C#,Wpf,应用程序成功添加和清除ColumnSeries WPF图表中的数据,但无法重新添加post Clear。按照其他地方的建议将DataContext设置为null之前并没有解决问题。以下是代码,感谢您的帮助: public partial class MainWindow : Window { public MainWindow() { dataToChart = new Input(); InitializeCompone

应用程序成功添加和清除ColumnSeries WPF图表中的数据,但无法重新添加post Clear。按照其他地方的建议将DataContext设置为null之前并没有解决问题。以下是代码,感谢您的帮助:

public partial class MainWindow : Window
{

    public MainWindow()
        {
            dataToChart = new Input();
            InitializeComponent();
            this.DataContext = dataToChart;
        }

    private void Button_Click_Clear(object sender, RoutedEventArgs e)
        {
            foreach (var series in columnChart.Series.OfType<Series>())
                {
                    series.DataContext = null;
                }
                columnChart.Series.Clear();
                dataToChart.Clear();
        }

    private void Button_Click_Add(object sender, RoutedEventArgs e)
        {
            dataToChart.Add(new KeyValuePair<string, int>("Test", 10));
        }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
dataToChart=新输入();
初始化组件();
this.DataContext=dataToChart;
}
私有无效按钮\u单击\u清除(对象发送方,路由目标)
{
foreach(columnChart.series.OfType()中的变量系列)
{
series.DataContext=null;
}
columnChart.Series.Clear();
dataToChart.Clear();
}
私有无效按钮\u单击\u添加(对象发送方,路由目标)
{
添加(新的键值对(“测试”,10));
}
}
Input.cs

public class Input
{
    public ObservableCollection<KeyValuePair<string, int>> ValueList { get; private set; }

    public Input()
    {
        this.ValueList = new ObservableCollection<KeyValuePair<string, int>>();
    }

    public void Add(KeyValuePair<string, int> data)
    {
        ValueList.Add(data);
    }

    public void Clear()
    {
        ValueList.Clear();
    }
}
公共类输入
{
公共ObservableCollection值列表{get;private set;}
公共投入()
{
this.ValueList=新的ObservableCollection();
}
public void添加(KeyValuePair数据)
{
价值清单。添加(数据);
}
公共空间清除()
{
ValueList.Clear();
}
}
MainWindow.xaml

<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
        x:Class="IES_Console.MainWindow"
        x:Name="wndMain" Title="IES Console" AllowsTransparency="True" WindowStyle="None"  WindowStartupLocation="Manual" ResizeMode="NoResize" Background="Transparent" Foreground="{x:Null}" Loaded="Window_Loaded" Closing="Window_Closing" KeyUp="Window_KeyUp" MouseLeftButtonDown="Window_MouseLeftButtonDown" SizeChanged="Window_SizeChanged" LocationChanged="Window_LocationChanged" Icon="/IES%20Console;component/48x48.ico" MouseLeave="wndMain_MouseLeave" mc:Ignorable="d" FontFamily="Arial" Width="Auto" Height="Auto" SizeToContent="WidthAndHeight">


    <Grid x:Name="MainGrid" Background="Black" ShowGridLines="False" Width="531" Height="700"  VerticalAlignment="Top" HorizontalAlignment="Left">

        <!--Code taken out for simplicity-->


        <Grid x:Name="subFooter" Grid.Row="3" Background =" Black" Margin="0,20,0,-144">


            <TextBlock x:Name="textBlock1"  VerticalAlignment="top" Margin="10,10,0,0" TextWrapping="Wrap" 
                Text="TextBlock" Foreground="White" Width="511" Height="50"/>

            <chartingToolkit:Chart Height="262" HorizontalAlignment="Left" 
                                   Margin="33,0,0,10" Name="columnChart" Title="Column Series Demo" 
                                   VerticalAlignment="Bottom" Width="360">

                <chartingToolkit:ColumnSeries  DependentValuePath="Value" Visibility="Visible"
                                        IndependentValuePath="Key" ItemsSource="{Binding ValueList}" />

                <chartingToolkit:Chart.LegendStyle>
                    <Style TargetType="Control">
                        <Setter Property="Width" Value="0" />
                        <Setter Property="Height" Value="0" />
                    </Style>
                </chartingToolkit:Chart.LegendStyle>

            </chartingToolkit:Chart>

            <Button Content="Data Clear" HorizontalAlignment="Left" Margin="426,65,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_Clear"/>
            <Button Content="Data Add" HorizontalAlignment="Left" Margin="426,108,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_Add"/>

        </Grid>
    </Grid>
</Window>

问题在于代码

private void Button_Click_Clear(object sender, RoutedEventArgs e)
{
    foreach (var series in columnChart.Series.OfType<Series>())
    {
        series.DataContext = null;
    }
    columnChart.Series.Clear();
    dataToChart.Clear();
}
当然,如果多个系列具有不同的
DataContext
,则需要将正确的
输入重新分配给相应的
series.DataContext

private void Button_Click_Clear(object sender, RoutedEventArgs e)
{
    dataToChart.Clear();
    foreach (var series in columnChart.Series.OfType<Series>())
    {
        series.DataContext = null;
        series.DataContext = dataToChart;
    }
}