【问题标题】:WPF - OxyPlot - Binding to DataPointsWPF - OxyPlot - 绑定到数据点
【发布时间】:2016-02-11 17:02:10
【问题描述】:

我正在开发一个使用 OxyPlot 的 WPF 应用程序。

我遵循了这个例子。我正在使用以下 XAML 成功绘制图表:

<oxy:Plot Height="640" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
  <oxy:Plot.Series>
    <oxy:LineSeries ItemsSource="{Binding ResultSet1}" />
    <oxy:LineSeries ItemsSource="{Binding ResultSet2}" />
    <oxy:LineSeries ItemsSource="{Binding ResultSet3}" />
  </oxy:Plot.Series>
</oxy:Plot>

我的 ViewModel 如下所示:

public class MyViewModel
{
  public IList<DataPoint> ResultSet1 { get; set; }

  public IList<DataPoint> ResultSet2 { get; set; }

  public IList<DataPoint> ResultSet3 { get; set; }

  public void Load()
  {
    this.ResultSet1 = new List<DataPoint>
    {
      new DataPoint(0, 4),
      new DataPoint(40, 12),
      new DataPoint(50, 12)
    };

    this.ResultSet2 = new List<DataPoint>
    {
      new DataPoint(-0.4, 3),
      new DataPoint(8, 12),
      new DataPoint(48, 11)
    };

    this.ResultSet3 = new List<DataPoint>
    {
      new DataPoint(2, 5),
      new DataPoint(12, 14),
      new DataPoint(52, 13)
    };
  }

  public void Refresh()
  {
    this.ResultSet1 = new List<DataPoint>();
    this.ResultSet2 = new List<DataPoint>();
    this.ResultSet3 = new List<DataPoint>();
    System.Windows.MessageBox.Show("should be empty");
  }
}

在我的代码中,我有一个“刷新”按钮。当用户单击它时,我正在尝试刷新显示的数据。但是,就像结果没有在 UI 中更新一样。我添加了上面显示的MessageBox,以确保我实际上进入了Refresh 方法。出现该消息框。所以,在这一点上,我知道:

a) OxyPlot 图表正在工作,因为我的硬编码初始结果集值看起来很好。 b) 我已经成功连接了视图模型。 c) 我正在使用Refresh 方法。

我只是不确定为什么图表上的点似乎并不令人耳目一新。任何见解都值得赞赏!

【问题讨论】:

  • 你应该为你的视图模型实现INotifyPropertyChanged;否则,视图中的绑定目标ItemsSource 仍然会监听旧对象的变化,并且不会知道属性被替换(刷新后指向新对象)。
  • :)。这些日子之一,我会记得INotifyPropertyChanged。不知为何,我总是忘记。非常感谢您的回复。
  • @Peter,我认为你应该写下问题的答案;)
  • 我总是在 ViewModel 中创建一个 PlotModel 属性并将它们绑定到 xaml,然后在此属性上调用 PlotModel.InvalidatePlot(true)。这将使用新数据渲染绘图,或者在您的情况下,它不会显示任何数据,因为列表是空的。

标签: wpf oxyplot


【解决方案1】:

不要使用IList,而是使用ObservableCollection

 public ObservableCollection<DataPoint> ResultSet1 { get; set; }
 public ObservableCollection<DataPoint> ResultSet2 { get; set; }
 public ObservableCollection<DataPoint> ResultSet3 { get; set; }

而不是使用new IList&lt;T&gt;() 使用

ResultSet1.Clear();
ResultSet2.Clear();
ResultSet3.Clear();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 2013-10-05
    • 2011-05-11
    • 1970-01-01
    相关资源
    最近更新 更多