WPF工具箱图中的动态数据

本文关键字:动态 数据 工具箱 WPF | 更新日期: 2023-09-27 18:09:26

我有一个LineSeries,我从WPFToolkit中使用。以下是我的XAML代码:

<Window x:Class="WpfChartExample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:chrt="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <chrt:Chart x:Name="simChart" Title="Simulation">
        <chrt:LineSeries IndependentValueBinding="{Binding Name}" 
                         DependentValueBinding="{Binding Value}" 
                         ItemsSource="{Binding}">
        </chrt:LineSeries>
    </chrt:Chart>
    <Button Name="btnStart" Width="Auto" Height="30" Grid.Row="1" HorizontalAlignment="Right" Margin="10" Click="btnStart_Click">Start Simulation</Button>
</Grid>

这是生成数据的类:

public partial class MainWindow : Window
{
    ObservableCollection<ChartData> chartData;
    ChartData objChartData;
    Thread thSim;
    bool thLock = true;
    public MainWindow()
    {
        InitializeComponent();
        chartData = new ObservableCollection<ChartData>();
        simChart.DataContext = chartData;
    }
    public void StartChartDataSimulation()
    {
        int i = 0;
        while (i < 10)
        {
            chartData.Add(new ChartData() { Name = DateTime.Now.ToString("HH:MM:ss"), Value = new Random().NextDouble() });
            Thread.Sleep(1000);
            i++;
        }
    }
    private void btnStart_Click(object sender, RoutedEventArgs e)
    {
        StartChartDataSimulation();            
    }

}
public class ChartData : INotifyPropertyChanged
{
    string _Name;
    double _Value;
    #region properties
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
            OnPropertyChanged("Name");
        }
    }
    public double Value
    {
        get
        {
            return _Value;
        }
        set
        {
            _Value = value;
            OnPropertyChanged("Value");
        }
    }
    #endregion
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

生成所有值后,图表更新。所以,图表一直是空白的。我希望图表尽快更新的价值是增加。我该怎么做呢?

WPF工具箱图中的动态数据

我想我知道答案了。我将代码修改如下:

public partial class MainWindow : Window
{
    ObservableCollection<ChartData> chartData;
    ChartData objChartData;
    Thread MyThread;
    public MainWindow()
    {
        InitializeComponent();
        chartData = new ObservableCollection<ChartData>();
        objChartData = new ChartData() { Name = DateTime.Now.Second.ToString(), Value = 0.0 };
        chartData.Add(objChartData);
        simChart.DataContext = chartData;
        MyThread = new Thread(new ThreadStart(StartChartDataSimulation));
    }
    void MainWindow_DataChanged()
    {
        throw new NotImplementedException();
    }
    public void StartChartDataSimulation()
    {
        while (true)
        {
            Dispatcher.Invoke(new Action(() =>
            {
                chartData.Add(new ChartData() { Name = DateTime.Now.Second.ToString(), Value = new Random().NextDouble() });
            }));
            //objChartData.Name = DateTime.Now.Second.ToString();
            //objChartData.Value = new Random().NextDouble();
            Thread.Sleep(500);
        }
    }
    private void btnStartStop_Click(object sender, RoutedEventArgs e)
    {
        if ((string)btnStartStop.Content == "Start Simulation")
        {
            if (MyThread.ThreadState == ThreadState.Unstarted)
            {
                MyThread.Start();
            }
            else if (MyThread.ThreadState == ThreadState.Suspended)
            {
                MyThread.Resume();
            }
            btnStartStop.Content = "Stop Simulation";
        }
        else
        {
            MyThread.Suspend();
            btnStartStop.Content = "Start Simulation";
        }
    }
    private void Window_Closing(object sender, CancelEventArgs e)
    {
        MyThread.Suspend();
        Application.Current.Shutdown();
    }

}
public class ChartData : INotifyPropertyChanged
{
    string _Name;
    double _Value;
    #region properties
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
            OnPropertyChanged("Name");
        }
    }
    public double Value
    {
        get
        {
            return _Value;
        }
        set
        {
            _Value = value;
            OnPropertyChanged("Value");
        }
    }
    #endregion
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}