wpf OxyPlot 中的多 LineSeries 绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17198926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple LineSeries Binding in OxyPlot
提问by Sturm
Is it possible to bind a plot to a collection of LineSeries instead of a single LineSeries in OxyPlot? (and not through the Model).
是否可以将绘图绑定到 LineSeries 的集合而不是 OxyPlot 中的单个 LineSeries?(而不是通过模型)。
I'm looking for something like this:
我正在寻找这样的东西:
<oxy:Plot>
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding myCollectionOfLineSeries}" />
</oxy:Plot.Series>
</oxy:Plot>
Where myCollectionOfLineSeries is:
其中 myCollectionOfLineSeries 是:
private ObservableCollection<LineSeries> _myCollectionOfLineSeries ;
public ObservableCollection<LineSeries> myCollectionOfLineSeries
{
get
{
return _myCollectionOfLineSeries ;
}
set
{
_myCollectionOfLineSeries = value;
OnPropertyChanged("myCollectionOfLineSeries ");
}
}
I expect as answer: a) "No, it's impossible" or b) "Yes, just put XYZ before IJK".
我希望答案是:a)“不,这是不可能的”或 b)“是的,只需将 XYZ 放在 IJK 之前”。
Thanks for reading.
谢谢阅读。
回答by Karl Cheng
It may be a bit late, but recently I have the same question: I need to plot multiple series dynamically (several yield curves based on user selected currencies) but I don't want to directly bind the Plotusing PlotModel, as other properties (e.g. Title) need to be set in my View Model as code instead of XAML markup.
可能有点晚了,但最近我有同样的问题:我需要动态绘制多个系列(基于用户选择的货币的多条收益率曲线)但我不想直接绑定Plotusing PlotModel,因为其他属性(例如Title)需要在我的视图模型中设置为代码而不是 XAML 标记。
So I defined the PlotModelas resource, binding it to the Plot. And look up the PlotModel when the view is loaded. By this approach, I can define visual stuffs (e.g. Title, Axes, Legend, etc) by XAML markup, while putting logic to generate series in my view model code.
所以我将其定义PlotModel为资源,将其绑定到 Plot。并在加载视图时查找 PlotModel。通过这种方法,我可以通过 XAML 标记定义视觉内容(例如标题、轴、图例等),同时在我的视图模型代码中放置生成系列的逻辑。
Not sure if it's a good way, but it solves my problem.
不确定这是否是一个好方法,但它解决了我的问题。
1) XAML - define resource
1) XAML - 定义资源
<UserControl.Resources>
<oxyPlot:PlotModel
x:Key="TestPlotModel"
Title="XXX Curves (Preview)"
Subtitle="Scroll mousewheel to zoom; Right-drag to pan"
LegendPlacement="Outside"
LegendBorder="{x:Static Member=oxyPlot:OxyColors.Black}"
>
<oxyPlot:PlotModel.Axes>
<axes:LinearAxis
Title="Rate (%)"
Position="Left"
StartPosition="0"
StringFormat="#.00000"
MajorGridlineStyle="Solid"
MajorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
MinorGridlineStyle="Dot"
MinorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
>
</axes:LinearAxis>
<axes:LinearAxis
Title="Maturity (Days)"
Position="Bottom"
StartPosition="0"
MajorGridlineStyle="Solid"
MajorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
>
</axes:LinearAxis>
</oxyPlot:PlotModel.Axes>
</oxyPlot:PlotModel>
</UserControl.Resources>
2) XAML - Plot
2) XAML - Plot
<oxy:Plot Grid.Row="1" Model="{Binding Source={StaticResource TestPlotModel}}">
</oxy:Plot>
3) View model - get the model from view but not binding
3)查看模型 - 从视图中获取模型但不绑定
protected override void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
this._model = (PlotModel)((XXXView)view).FindResource("TestPlotModel");
}
4) View model - generate multiple series
4)查看模型——生成多个系列
_model.Series.Clear();
foreach (var currency in distinctCurrencies)
{
IEnumerable<DataPoint> dataPoints = ...;
LineSeries lineSeries = new LineSeries()
{
Title = String.Format("*{0}", currency),
ItemsSource = dataPoints
};
_model.Series.Add(lineSeries);
}
hope it helps!
希望能帮助到你!
回答by devdigital
Yes, have a look at their examples, you need to bind to a collection of DataPoint
是的,看看他们的例子,你需要绑定到一个集合DataPoint
public ObservableCollection<DataPoint> MyCollection { ... }
and
和
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding MyCollection}" DataFieldX="X" DataFieldY="Y"/>
</oxy:Plot.Series>
The Seriesproperty on the Plot typehas no setter:
Plot 类型的Series属性没有 setter:
public Collection<Series> Series
{
get
{
return this.series;
}
}
You can though bind to the Modelproperty, which is a PlotModeltype which has a Seriescollection property with a getter and setter. Have a look at the SimpleDemofor more details.
您可以绑定到Model属性,该属性是一种PlotModel类型,它具有Series带有 getter 和 setter的集合属性。查看SimpleDemo了解更多详细信息。
<oxy:Plot Model="{Binding MyModel}" ...

