【发布时间】:2017-07-20 16:08:37
【问题描述】:
我在 WPF 中使用 OxyPlot 并且遇到了一些问题。我正在尝试创建一个应用程序并希望使用 OxyPlot 创建一个图表。一切正常,除了情节/数据不会显示。我似乎无法弄清楚为什么。
这是我的一些 xaml 代码:
<UserControl x:Class="myNameSpace.MainPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:app="clr-namespace:myNameSpace"
xmlns:oxy="http://oxyplot.org/wpf"
mc:Ignorable="d"
d:DesignHeight="1200"
d:DesignWidth="1920">
<UserControl.DataContext>
<local:MainViewModel/>
</UserControl.DataContext>
....
<oxy:Plot Title="{Binding Title}" Margin="760,689,606,228" Width="504" Height="283">
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding Points}"/>
</oxy:Plot.Series>
</oxy:Plot>
我的 MainViewPanel 类看起来像这样:
public class MainViewModel
{
public MainViewModel()
{
this.Title = "Example 2";
this.Points = new List<DataPoint>
{
new DataPoint(0, 4),
new DataPoint(10, 13),
new DataPoint(20, 15),
new DataPoint(30, 16),
new DataPoint(40, 12),
new DataPoint(50, 12)
};
}
public string Title { get; private set; }
public IList<DataPoint> Points { get; private set; }
}
这是我运行代码时图表的样子
【问题讨论】:
-
您提供的代码对我来说没问题。这是一个外部机会,但我假设 VM 中的点列表是 OxyPlot.DataPoint 的列表,而不是来自其他地方的 DataPoint 的列表。
-
忽略我之前的评论,再看你的图,标题好像也没绑定。
-
您确定 xaml 中引用的 MainViewModel 类型正确吗?我没有看到定义的本地命名空间前缀。如果 MainViewModel 在 AnnaEmilie 命名空间中,请将 local:MainViewModel 更改为 app:MainViewModel
-
非常感谢您的帮助!问题在于本地命名空间前缀。现在可以正常使用了。
-
找到答案真是太好了。我认为如果有人将答案发布到 Q 会很好。