Oxyplot - WPF 使 plotview 无效

Oxyplot - WPF invalidate plotview

我正在为我的 WPF 应用程序使用 Oxyplot。我想捕获一些数据,然后我想在图表中显示它们。 我在 XAML:

中有这段代码
xmlns:oxy="http://oxyplot.org/wpf"


 <Window.DataContext>
        <local:MainViewModel/>
  </Window.DataContext>
  <oxy:PlotView Title="{Binding Title}" Margin="241,0,0,179" Name="Plot1" Grid.Column="2">
       <oxy:PlotView.Series>
            <oxy:LineSeries ItemsSource="{Binding Points}"/>
        </oxy:PlotView.Series>
  </oxy:PlotView>

和Class:

public class MainViewModel
{
    /// <summary>
    /// 
    /// </summary>
    /// 
    public MainViewModel()
    {
        this.Title = "Sin";

        this.Points = new List<DataPoint>();

        for (int i = 0; i <= 800; i++)
        {
            double x = (Math.PI * i) / 400;
            double y = Math.Sin(x);
            DataPoint p = new DataPoint(x, y);
            Points.Add(p);
        }
        /*this.Points = new List<DataPoint>
         {
                              new DataPoint(0, 4),
                              new DataPoint(10, 13),
                              new DataPoint(20, 15),
                              new DataPoint(30, 16),
                              new DataPoint(40, 12),
                              new DataPoint(50, 12)
                          };*/
    }

    public string Title { get; private set; }

    public IList<DataPoint> Points { get; private set; }

    public void Second()
    {
        this.Points.Clear();
        this.Title = "Test";

        this.Points = new List<DataPoint>();

        for (int i = 0; i <= 800; i++)
        {
            double x = (Math.PI * i) / 400;
            double y = Math.Cos(x);
            DataPoint p = new DataPoint(x, 0.5);
            Points.Add(p);
        }
    }

}

事实是,我想要在单击按钮后显示 "Second()" 中的绘图。 它通过调用方法 Second() 和 Plot1.InvalidatePlot(true) 之后执行,但它什么也不做。 我哪里做错了,请帮忙? 谢谢

改为<oxy:LineSeries ItemsSource="{Binding Points, Mode=OneWay}"/>

这将确保对 Points 的更改传播到您的图表控件。其次,在您的 ViewModel class 中实施 INotifyPropertyChanged。例如;

IList<DataPoint> _points;
public IList<DataPoint> Points { get{return _points;}; private set{ _points = value; OnPropertyChanged("Points");} }

        private void OnPropertyChanged(string p)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));
        }

public void Second()
    {
        this.Points.Clear();
        this.Title = "Test";

        this.Points = new List<DataPoint>();

        for (int i = 0; i <= 800; i++)
        {
            double x = (Math.PI * i) / 400;
            double y = Math.Cos(x);
            DataPoint p = new DataPoint(x, 0.5);
            Points.Add(p);
        }
        /* suggested change */
        OnPropertyChanged("Points");
    }

目前,答案可能会改变,已经mentioned here

  • Change the Model property of the PlotView control
  • Call Invalidate on the PlotView control
  • Call Invalidate on the PlotModel

我建议您删除 "PropertyChanged" 并将其插入到更新方法中。

PlotModel.InvalidatePlot(true);