OxyPlot WPF不使用按钮单击

OxyPlot WPF不使用按钮单击,wpf,buttonclick,oxyplot,Wpf,Buttonclick,Oxyplot,我有一些OxyPlot的问题,我无法通过他们的文档或其他搜索来解决。我正在开发一个wpf应用程序,它允许用户通过一个按钮点击事件打开一个.csv,然后执行一些数学运算并报告一些有用的信息。我想绘制一些生成的数据。由于某些原因,当生成它的代码在button click事件中时,我无法获取要填充的绘图。以下是一个较小的示例: 此代码有效(xaml): 为此: public partial class MainWindow : Window { public MainWindow()

我有一些OxyPlot的问题,我无法通过他们的文档或其他搜索来解决。我正在开发一个wpf应用程序,它允许用户通过一个按钮点击事件打开一个.csv,然后执行一些数学运算并报告一些有用的信息。我想绘制一些生成的数据。由于某些原因,当生成它的代码在button click事件中时,我无法获取要填充的绘图。以下是一个较小的示例: 此代码有效(xaml):


为此:

public partial class MainWindow : Window
{
    public MainWindow()
    {
       InitializeComponent();
        DataContext = this;
        var tmp = new PlotModel { Title = "Scatter plot", Subtitle = "y = x" };
        var s2 = new LineSeries
        {
            StrokeThickness = 1,
            MarkerSize = 1,
            MarkerStroke = OxyColors.ForestGreen,
            MarkerType = MarkerType.Plus
        };

        for (int i = 0; i < 100; i++)
        {
            s2.Points.Add(new DataPoint(i, i));
        }
        tmp.Series.Add(s2);
        this.ScatterModel = tmp;
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {

    }
    public PlotModel ScatterModel { get; set; }
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=this;
var tmp=new PlotModel{Title=“散点图”,Subtitle=“y=x”};
var s2=新的线系列
{
冲程厚度=1,
MarkerSize=1,
MarkerStroke=OxyColors.ForestGreen,
MarkerType=MarkerType.Plus
};
对于(int i=0;i<100;i++)
{
s2.点。添加(新数据点(i,i));
}
tmp系列增补(s2);
this.ScatterModel=tmp;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
}
公共PlotModel ScatterModel{get;set;}
并产生如下结果:

但是,在不更改xaml的情况下,如果复制/粘贴按钮单击事件下的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
       InitializeComponent();

    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        DataContext = this;
        var tmp = new PlotModel { Title = "Scatter plot", Subtitle = "y = x" };
        var s2 = new LineSeries
        {
            StrokeThickness = 1,
            MarkerSize = 1,
            MarkerStroke = OxyColors.ForestGreen,
            MarkerType = MarkerType.Plus
        };

        for (int i = 0; i < 100; i++)
        {
            s2.Points.Add(new DataPoint(i, i));
        }
        tmp.Series.Add(s2);
        this.ScatterModel = tmp;
    }
    public PlotModel ScatterModel { get; set; }
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
DataContext=this;
var tmp=new PlotModel{Title=“散点图”,Subtitle=“y=x”};
var s2=新的线系列
{
冲程厚度=1,
MarkerSize=1,
MarkerStroke=OxyColors.ForestGreen,
MarkerType=MarkerType.Plus
};
对于(int i=0;i<100;i++)
{
s2.点。添加(新数据点(i,i));
}
tmp系列增补(s2);
this.ScatterModel=tmp;
}
公共PlotModel ScatterModel{get;set;}
该绘图从未生成:

我已经尝试过将DataContext=this;备份到public main window(),反之亦然,使用InitializeComponent();没有更改。我还尝试过定义

<Window.DataContext>
    <local:MainWindow/>
</Window.DataContext>

在xaml中,但在生成期间引发异常/无限循环错误。 一些简单的东西,我担心我不了解OxyPlot的实现

谢谢!
CSMDakota

INotifyPropertyChanged
使您的视图与程序的状态保持同步。一种方法是实现
ViewModel
(MVVM模式)

因此,让我们创建一个。
ViewModelBase
介绍了更新
ScatterModel
的方法
OnPropertyChanged()

ViewModels.cs

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using OxyPlot;

namespace WpfApplication1
{
    public class ViewModel : ViewModelBase
    {
        private PlotModel _scatterModel;
        public PlotModel ScatterModel
        {
            get { return _scatterModel; }
            set
            {
                if (value != _scatterModel)
                {
                    _scatterModel = value;
                    OnPropertyChanged();
                }
            }
        }
    }

    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] String propName = null)
        {
            // C#6.O
            // PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
        }
    }
}
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        var tmp = new PlotModel { Title = "Scatter plot", Subtitle = "y = x" };
        var s2 = new LineSeries
        {
            StrokeThickness = 1,
            MarkerSize = 1,
            MarkerStroke = OxyColors.ForestGreen,
            MarkerType = MarkerType.Plus
        };

        for (int i = 0; i < 100; i++)
        {
            s2.Points.Add(new DataPoint(i, i));
        }
        tmp.Series.Add(s2);
        ViewModel.ScatterModel = tmp;
    }

    // C#6.O
    // public ViewModel ViewModel => (ViewModel)DataContext;
    public ViewModel ViewModel
    {
        get { return (ViewModel)DataContext; }
    }
}
main window.xaml中,您现在可以添加

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>

MainWindow.xaml.cs

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using OxyPlot;

namespace WpfApplication1
{
    public class ViewModel : ViewModelBase
    {
        private PlotModel _scatterModel;
        public PlotModel ScatterModel
        {
            get { return _scatterModel; }
            set
            {
                if (value != _scatterModel)
                {
                    _scatterModel = value;
                    OnPropertyChanged();
                }
            }
        }
    }

    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] String propName = null)
        {
            // C#6.O
            // PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
        }
    }
}
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        var tmp = new PlotModel { Title = "Scatter plot", Subtitle = "y = x" };
        var s2 = new LineSeries
        {
            StrokeThickness = 1,
            MarkerSize = 1,
            MarkerStroke = OxyColors.ForestGreen,
            MarkerType = MarkerType.Plus
        };

        for (int i = 0; i < 100; i++)
        {
            s2.Points.Add(new DataPoint(i, i));
        }
        tmp.Series.Add(s2);
        ViewModel.ScatterModel = tmp;
    }

    // C#6.O
    // public ViewModel ViewModel => (ViewModel)DataContext;
    public ViewModel ViewModel
    {
        get { return (ViewModel)DataContext; }
    }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
var tmp=new PlotModel{Title=“散点图”,Subtitle=“y=x”};
var s2=新的线系列
{
冲程厚度=1,
MarkerSize=1,
MarkerStroke=OxyColors.ForestGreen,
MarkerType=MarkerType.Plus
};
对于(int i=0;i<100;i++)
{
s2.点。添加(新数据点(i,i));
}
tmp系列增补(s2);
ViewModel.ScatterModel=tmp;
}
//C#6.O
//公共ViewModel ViewModel=>(ViewModel)数据上下文;
公共视图模型视图模型
{
获取{return(ViewModel)DataContext;}
}
}

注意,我们不再设置
DataContext=this
,这被认为是不好的做法。在这种情况下,
ViewModel
很小,但随着程序的发展,这种结构化方式会得到回报。

它与OxyPlot无关。您需要实现
INotifyPropertyChanged
,这样您的
ScatterModel
点击按钮后更新。循环来自行
。在MVVM中,它将是
。Funk,太棒了!感谢您的详细回答,我按照您的说明操作,现在它正在按预期工作。在我返回主程序之前,我将确保我了解您在这里做了什么,我仍然需要仔细研究它完全正确。谢谢!