1

I have two oxyplot charts (a lineseries and a rectanglebarseries) in my project. However, I can only display one of them at the same time. I know it is because of how I set the DataContext, but I do not know how to change my code so that both of the charts can be displayed at the same time. How can I achieve that?

The xaml-code of my mainpanel:

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

Parts of my cs-classes:

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

The 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();
    }
3
  • i'm a tad confused, from the xaml i see in your mainPanel, i'd say you already have it set up properly. all you would need to do is pass it a viewmodel that contains the PlotModel and HistoryModel, then set the datacontext to that viewmodel. Commented Jul 10, 2017 at 8:49
  • Hi @TimothyGroote! How do I set up a viewmodel? I am new to wpf and c#. Thank you for understanding. Commented Jul 10, 2017 at 8:52
  • Basically, a viewmodel is just a class, like your TrendModel. in WPF, it may or may not notify the framework of changing dependency properties using constructs like INotifyPropertyChanged Commented Jul 10, 2017 at 8:59

2 Answers 2

1

Set the DataContext property of each PlotView:

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

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

Or define the PlotModel and HistogramModel properties in the same view model class and set the DataContext property of the view to an instance of this class.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi mm8, I do not seem to have a DataContext property for my viewmodels. Plot.DataContext and Histogram.DataContext gives the error: myNameSpace.myClass does not contain a definition for DataContext. Should I add something to my classes? Thank you!
These are the names of the controls in your view: x:Name="Plot". You should probably rename these to something different than the names of your types.
Oh. THANK YOU! It works perfectly now and I have changed the names to something less confusing!
1

You should put both the models that are passed to the oxyplot renderers in a separate viewmodel, then use that as the datacontext for your mainPanel.

something like this :

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

Basically, the rest should go like this :

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

DataContext = vm;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.