wpf - OxyPlot 图表中的可绑定注释
问题描述
我正在尝试使用 OxyPlot 来可视化时间序列,并且我正在使用注释来标记一些时间点和值级别,例如最小值/最大值等。
我想通过绑定在 WPF 中实现这一点,到目前为止,我从Plot
控件、两个轴和一个数据系列开始。这很好用,但我找不到将注释绑定到ObservableCollection<Annontation>
动态创建的各种注释的方法。
<UserControl
...
xmlns:oxy="http://oxyplot.org/wpf"
...
>
<Grid>
...
<oxy:Plot>
<oxy:Plot.Axes>
<oxy:DateTimeAxis ... />
<oxy:LinearAxis ... />
</oxy:Plot.Axes>
<oxy:Plot.Series>
<oxy:StairStepSeries ... />
</oxy:Plot.Series>
<oxy:Plot.Annotations>
<!--
How do I bind to the ObservableCollection 'Annotations' in my ViewModel?
Note, 'Annotations' property isn't available directly in Plot, either.
-->
</oxy:Plot.Annotations>
</oxy:Plot>
</Grid>
</UserControl>
解决方案
我遇到了类似的问题。只是为其他人提供答案,如何使用 OxyPlot 处理这个问题:
如果您有未知数量的注释或系列,您应该使用
<oxy:PlotView Model="{Binding PlotModel}" />
在 Xaml 和 ViewModel 上的这个属性
public PlotModel PlotModel { get; set; }
在此属性上,您具有更大的灵活性。您不能PlotModel.Annotations
像 一样设置LineSeries.ItemSource
,但您可以随时添加和删除您需要的这么多注释。