c# - 在 WPF 中使用 Oxyplot 进行数据绑定(bind)

标签 c# wpf xaml data-binding oxyplot

我正在努力解决 WPF 项目中有关 OxyPlot 的几个问题。

首先,我可以使用 Plot 类或 PlotView 类。这两个类有什么区别?

理想情况下,我想对模型(或至少模型的一部分)和数据使用数据绑定(bind)。

如果我使用 PlotView,我可以为模型使用 Binding,如下所示:

<oxy:PlotView Model="{Binding Model}"/>

如果我使用 Plot,我可以对数据使用数据绑定(bind),比如

<oxy:Plot>
  <oxy:Plot.Series>
    <oxy:LineSeries ItemsSource="{Binding Points}" />
  </oxy:Plot.Series>
</oxy:Plot>

我可以让这两个都起作用,但是有没有办法对模型和数据使用绑定(bind)?

如果我对数据使用 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>

我根本无法让它工作。曲线始终为绿色。我的 LineColor 属性定义为 OxyColor 类型。这是错误的类型吗?

我知道我在同一个帖子中问了几个问题,但我认为它们密切相关。

最佳答案

首先,我可以使用 Plot 类或 PlotView 类。这两个类有什么区别?

我认为您在示例中看到了差异,如果您想绑定(bind)到模型,则必须使用 oxy:PlotView。如果你想绑定(bind)到 LineSeries,那么你将不得不使用 oxy:Plot 控件。

我可以让这两个都起作用,但是有没有办法对模型和数据使用绑定(bind)?

不,如上一句所述,您不能同时绑定(bind)两者,但您可以像这样(在您的示例中)将 lineseries 添加到您的模型中:

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

然后使用 oxy:PlotView 绑定(bind)到模型,仅此而已。如果您想修改处理绘图行为的参数,则必须将 PlotController 属性绑定(bind)到(以防万一,以备将来使用)。

编辑:

Oystein Bjorke(OxyPlot 创建者)这样回答两个问题:

The PlotView component is now similar on all platforms, it contains only Model and Controller properties. The Plot control let you define axes, series, annotations etc. and this should only be available in XAML-based platforms.

关于c# - 在 WPF 中使用 Oxyplot 进行数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37307565/

相关文章:

c# - 如何使用 C# 禁用 Windows 文本框上的右键单击上下文菜单?

c# - 将 XML 文件加载到数据表中(而不是从数据库中)

wpf - 在 WPF 中使用 FindName() 找不到动态生成的控件

c# - WPF绑定(bind)不适用于ICommand

c# - WPF 从多个 View 模型将选项卡项添加到选项卡控件

c# - 第一次构建 ViewModel

c# - Office 加载项功能区 : same tab with 2 addins

c# - Json 对象到 xamarin ListView

c# - ApplicationBar 绑定(bind) Windows Phone

c# - 如何在 C# 中读取 Windows.UI.XAML.Style 属性