显示多个oxyplot图表c#wpf

时间:2017-07-10 08:44:50

标签: 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;

我的部分课程:

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);
    }

lineseries 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();
    }

2 个答案:

答案 0 :(得分:1)

设置每个DataContext的{​​{1}}属性:

PlotView

或者在同一个视图模型类中定义trendModel = new TrendModel("VariableName"); Plot.DataContext = trendmodel; Histogram histogram = new Histogram(freq_List, axis_List); Histogram.DateContext = histogram; PlotModel属性,并将视图的HistogramModel属性设置为此类的实例。

答案 1 :(得分:0)

您应该将传递给oxyplot渲染器的模型放在单独的视图模型中,然后使用 作为mainPanel的datacontext。

类似的东西:

//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;