【问题标题】:Create an OxyPlot in WPF在 WPF 中创建 OxyPlot
【发布时间】: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 会很好。

标签: c# wpf oxyplot


【解决方案1】:

我没有看到定义的本地命名空间前缀。如果 MainViewModel 在 AnnaEmilie 命名空间中,请将 local:MainViewModel 更改为 app:MainViewModel

【讨论】:

    【解决方案2】:

    删除边距,因为它们会使您的图表变得不可见。

    <oxy:Plot Title="{Binding Title}" Width="504" Height="283">
        <oxy:Plot.Series>
            <oxy:LineSeries ItemsSource="{Binding Points}"/>
        </oxy:Plot.Series>
    </oxy:Plot>
    

    【讨论】:

    • 不确定这是否能回答 Anna 的问题。她正在查看图表,它只是没有任何数据点。我认为大的边距被用于绝对定位。
    • 你说得对@JanisS,当我现在使用边距时,我只能看到我的图表的一半(它以前工作过)。但是,如果我删除它,我将无法获得绝对定位,并且图表最终不会出现在我想要的位置。
    • @Anna 尝试使用相对定位。最简单的方法之一是“堆叠”您的元素,然后使用边距和填充将它们定位为您所期望的或使用 Grid (codeproject.com/Articles/140613/…)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多