【发布时间】: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之类的结构更改依赖项属性