显示多个 oxyplot 图表 c# wpf

Display multiple oxyplot charts c# wpf

我的项目中有两个 oxyplot 图表(一个 lineseries 和一个 rectanglebarseries)。但是,我只能同时显示其中一个。我知道这是因为我如何设置 DataContext,但我不知道如何更改我的代码以便同时显示两个图表。我怎样才能做到这一点?

我的主面板的xaml代码:

        <oxy:PlotView x:Name="Plot" Model="{Binding PlotModel}" Margin="171,648,407,0" Background="MistyRose"/>
        <oxy:PlotView x:Name ="Histogram" Model="{Binding HistogramModel}" Margin="445,304,78,459" Background="AliceBlue"/>

mainpanel.cs

...
    trendModel = new TrendModel("VariableName");
    DataContext = trendmodel;
    Histogram histogram = new Histogram(freq_List, axis_List);
    DateContext = histogram;

我的部分 cs-类:

namespace ...
{
    public class Histogram : INotifyPropertyChanged
    {
    public Collection<Item> Items { get; set; }
    private PlotModel histogramModel;
    public PlotModel HistogramModel //{ get; set; }
    {
        get { return histogramModel; }
        set { histogramModel = value; OnPropertyChanged("HistogramModel"); }
    }

    public class Item
    {
        public string Label { get; set; }
        public double Value { get; set; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    //NotifyPropertyChangedInvocator
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public Histogram(List<double> frequency, List<double> axis)
    {
        CreateRectangleBar(frequency, axis);
    }

线条 cs:

namespace ...
{
    public class TrendModel : INotifyPropertyChanged
    {
        private PlotModel plotModel;
        public PlotModel PlotModel
    {
        get { return plotModel; }
        set { plotModel = value; OnPropertyChanged("PlotModel"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    //NotifyPropertyChangedInvocator
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    //Constructor
    public TrendModel(string Name)
    {
        PlotModel = new PlotModel() { Title = Name };
        SetUpModel();
    }

您应该将传递给 oxyplot 渲染器的两个模型放在单独的视图模型中,然后使用 that 作为 mainPanel 的数据上下文。

像这样:

//you might want to implement INotifyPropertyChanged for viewmodel classes. 
//i did not do so in this example.
public class MainPanelViewmodel 
{
   public TrendModel PlotModel { get; set; }
   public Histogram HistogramModel { get; set; }
}

基本上,剩下的应该是这样的:

MainPanelViewmodel vm = new MainPanelViewmodel()
{
    PlotModel  = new TrendModel("VariableName"),
    HistogramModel = new Histogram(freq_List, axis_List)
}

DataContext = vm;

设置每个PlotViewDataContext属性:

trendModel = new TrendModel("VariableName");
Plot.DataContext = trendmodel;

Histogram histogram = new Histogram(freq_List, axis_List);
Histogram.DateContext = histogram;

或者在同一个视图模型中定义PlotModelHistogramModel属性class并将视图的DataContext属性设置为一个实例这个class.