簡體   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