wpf WPF中与Oxyplot的数据绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37307565/
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
Data binding with Oxyplot in WPF
提问by Phil Jollans
I am struggling with several issues regarding OxyPlot in a WPF project.
我正在努力解决 WPF 项目中有关 OxyPlot 的几个问题。
First off, I can use the Plot class or the PlotView class. What is the difference between these two classes?
首先,我可以使用 Plot 类或 PlotView 类。这两个类有什么区别?
Ideally, I want to use data binding for the Model (or at least parts of it) and for the data.
理想情况下,我想对模型(或至少其中的一部分)和数据使用数据绑定。
If I use PlotView, I can use Binding for the model, something like this:
如果我使用 PlotView,我可以为模型使用绑定,如下所示:
<oxy:PlotView Model="{Binding Model}"/>
If I use Plot, I can use data binding for the data, something like
如果我使用 Plot,我可以对数据使用数据绑定,比如
<oxy:Plot>
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding Points}" />
</oxy:Plot.Series>
</oxy:Plot>
I can get both of these to work, but is there a way to use Binding for both the Model and the Data?
我可以让这两个都工作,但是有没有办法对模型和数据使用绑定?
If I use the Plot class and Binding for the data, I would at least like to use Binding for the LineColor, like this
如果我对数据使用 Plot 类和 Binding,我至少想对 LineColor 使用 Binding,就像这样
<oxy:Plot>
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding Points}"
DataFieldX="X"
DataFieldY="Y"
StrokeThickness="2"
MarkerSize="0"
LineStyle="Solid"
Color="{Binding LineColor}"
MarkerType="None"/>
</oxy:Plot.Series>
</oxy:Plot>
I can't get this to work at all. The curve is always green. My LineColor property is defined with the type OxyColor. Is this the wrong type?
我根本无法让这个工作。曲线始终为绿色。我的 LineColor 属性定义为 OxyColor 类型。这是错误的类型吗?
I know that I have asked several questions in the same posting, but I think that they are very closely related.
我知道我在同一个帖子中问了几个问题,但我认为它们非常相关。
回答by Jose
First off, I can use the Plot class or the PlotView class. What is the difference between these two classes?
首先,我可以使用 Plot 类或 PlotView 类。这两个类有什么区别?
I think you are seeing the diference in your examples, if you want to bind to the model, you have to use oxy:PlotView. If you want to bind to LineSeries, then you will have to use oxy:Plotcontrol.
我认为您在示例中看到了差异,如果您想绑定到模型,则必须使用oxy:PlotView. 如果要绑定到LineSeries,则必须使用oxy:Plot控件。
I can get both of these to work, but is there a way to use Binding for both the Model and the Data?
我可以让这两个都工作,但是有没有办法对模型和数据使用绑定?
No, as stated in the last sentence, you cannot bind both at the same time, but you can add lineseries to your model like this (in your example):
不,如最后一句所述,您不能同时绑定两者,但您可以像这样(在您的示例中)向模型添加线系列:
PlotModel model = new PlotModel();
List<DataPoint> Points = new List<DataPoint>();
LineSeries lineserie = new LineSeries
{
ItemsSource = Points,
DataFieldX = "x",
DataFieldY = "Y",
StrokeThickness = 2,
MarkerSize = 0,
LineStyle = LineStyle.Solid,
Color = OxyColors.White,
MarkerType = MarkerType.None,
};
model.Series.Add(lineserie);
Then you bind to the model, using oxy:PlotView, and that's all. If you want to modify parameters that deals with plot behaviour, you have to bind to PlotControllerproperty to (just in case, for future work).
然后您使用 绑定到模型,仅oxy:PlotView此而已。如果要修改处理绘图行为的参数,则必须将PlotController属性绑定到(以防万一,以备将来工作)。
EDIT:
编辑:
Oystein Bjorke (OxyPlot creator) said this, answering both questions:
Oystein Bjorke(OxyPlot 的创建者)回答了这两个问题:
The
PlotViewcomponent is now similar on all platforms, it contains onlyModelandControllerproperties. ThePlotcontrol let you define axes, series, annotations etc. and this should only be available in XAML-based platforms.
该
PlotView组件现在在所有平台上都是相似的,它只包含Model和Controller属性。该Plot控件让您可以定义轴、系列、注释等,这应该仅在基于 XAML 的平台中可用。

