【问题标题】:Rebinding Datacontext to update oxyplot graph重新绑定 Datacontext 以更新 oxyplot 图
【发布时间】:2019-06-01 20:48:00
【问题描述】:

我希望能够刷新由 Oxyplot 创建的图表,但我很难做到这一点。

理论上 this.model.InvalidatePlot(true);应该更新它,但它不想。

MainPage.xaml.cs

using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;

namespace WeatherStation
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.BuildModel2();
            this.BuildModel();
        }

        public void BuildModel()
        {
            this.model = new PlotModel();

            LineSeries lineserie = new LineSeries();
            lineserie.Points.Add(new DataPoint(0, 0));
            lineserie.Points.Add(new DataPoint(1, 70));
            lineserie.Points.Add(new DataPoint(2, 20));
            lineserie.Points.Add(new DataPoint(3, 20));

            this.model.Series.Add(lineserie);
            this.model.InvalidatePlot(true);
            DataContext = this;
        }

        public void BuildModel2()
        {
            this.model = new PlotModel();

            LineSeries lineserie = new LineSeries();
            lineserie.Points.Add(new DataPoint(0, 0));
            lineserie.Points.Add(new DataPoint(1, 10));
            lineserie.Points.Add(new DataPoint(2, 80));
            lineserie.Points.Add(new DataPoint(3, 20));

            this.model.Series.Add(lineserie);
            this.model.InvalidatePlot(true);
            DataContext = this;
        }
        public PlotModel model { get; set; }
    }
}

MainPage.xaml

<Page
    x:Class="WeatherStation.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WeatherStation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:oxy="using:OxyPlot.Windows"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
        <oxy:PlotView Model="{Binding model, Mode=OneWay}"/>
    </Grid>
</Page>

此代码背后的总体思路是绘制第一张图,然后下一张应覆盖它。如果发生这种情况,我知道我可以在未来通过触发类似的函数来更新它。

【问题讨论】:

    标签: c# uwp binding datacontext oxyplot


    【解决方案1】:

    我已经修好了。我太笨了,看不到解决方案。

    首先我们需要这个

    public LineSeries lineserie { get; set; }
    

    我们将它添加到两个 BuildModel 中:

        public void BuildModel()
        {
            this.model = new PlotModel();
    
            this.lineserie = new LineSeries();
            this.lineserie.Points.Add(new DataPoint(0, 0));
            this.lineserie.Points.Add(new DataPoint(1, 70));
            this.lineserie.Points.Add(new DataPoint(2, 20));
            this.lineserie.Points.Add(new DataPoint(3, 20));
    
            this.model.Series.Add(lineserie);
            this.DataContext = this;
        }
    

    然后我们删除旧的系列并添加一个新系列,然后我们更新:)

        public void BuildModel2()
        {
            this.model.Series.Remove(this.lineserie);
    
            this.lineserie = new LineSeries();
            this.lineserie.Points.Add(new DataPoint(0, 0));
            this.lineserie.Points.Add(new DataPoint(1, 10));
            this.lineserie.Points.Add(new DataPoint(2, 80));
            this.lineserie.Points.Add(new DataPoint(3, 20));
            this.model.Series.Add(lineserie);
            this.model.InvalidatePlot(true);
        }
    

    我们很高兴。它基本上一直在使用同一个对象,同时更新所述对象,而不创建新对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 2010-09-23
      • 2015-09-18
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      相关资源
      最近更新 更多