繁体   English   中英

Oxyplot - WPF 使绘图视图无效

[英]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>

和类:

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) 之后执行,但它什么也不做。 我哪里做错了,请帮忙? 谢谢

目前,答案可能会改变, 这里已经提到了

  • 更改 PlotView 控件的 Model 属性
  • 在 PlotView 控件上调用 Invalidate
  • 在 PlotModel 上调用 Invalidate

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

PlotModel.InvalidatePlot(true);

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

这将确保对 Points 的更改传播到您的图表控件。 其次,在您的 ViewModel 类中实现 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");
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM