【问题标题】:Display multiple oxyplot charts c# wpf显示多个oxyplot图表c# wpf
【发布时间】:2017-07-10 17:26:51
【问题描述】:

我的项目中有两个 oxyplot 图表(一个 lineeries 和一个 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();
    }

【问题讨论】:

  • 我有点困惑,从我在你的 mainPanel 中看到的 xaml 来看,我想说你已经正确设置了它。您需要做的就是向它传递一个包含 PlotModel 和 HistoryModel 的视图模型,然后将数据上下文设置为该视图模型。
  • 嗨@TimothyGroote!如何设置视图模型?我是 wpf 和 c# 的新手。感谢您的理解。
  • 基本上,视图模型只是一个类,就像你的TrendModel。在 WPF 中,它可能会或可能不会通知框架使用 INotifyPropertyChanged 之类的结构更改依赖项属性

标签: c# wpf oxyplot


【解决方案1】:

设置每个PlotViewDataContext属性:

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

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

或者在同一个视图模型类中定义PlotModelHistogramModel属性,并将视图的DataContext属性设置为该类的实例。

【讨论】:

  • 您好 mm8,我的视图模型似乎没有 DataContext 属性。 Plot.DataContext 和 Histogram.DataContext 给出错误:myNameSpace.myClass 不包含 DataContext 的定义。我应该在我的课程中添加一些东西吗?谢谢!
  • 这些是您视图中控件的名称:x:Name="Plot"。您可能应该将它们重命名为与您的类型名称不同的名称。
  • 哦。谢谢你!它现在完美运行,我已将名称更改为不那么令人困惑的名称!
【解决方案2】:

您应该将传递给 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多