【问题标题】:How to remove or hide comments on an OxyPlot graph?如何删除或隐藏 OxyPlot 图表上的注释?
【发布时间】:2020-03-18 07:45:39
【问题描述】:

如何删除或隐藏OxyPlot 图表上的 cmets?我正在这样做,但它不起作用:

public void AddAnnotation(IEnumerable<Annotation> annotations)
{
  foreach (var annotation in annotations)
  {
    MyOxyPlotModel.Annotations.Add(annotation);
  }

  RefreshAxisSeriesPlot();
}

public void RemoveAnnotation(IEnumerable<Annotation> annotations)
{
  foreach (var annotation in annotations)
  {
    MyOxyPlotModel.Annotations.Remove(annotation);
  }

  RefreshAxisSeriesPlot();
}

private void RefreshAxisSeriesPlot() => MyOxyPlotModel.InvalidatePlot(true);

使用此代码,添加注释有效,但删除注释不起作用。

编辑:

好的,我在代码中发现了问题。 事实上,我还没有完成对我的 LINQ 查询的评估,我从中得到了我的 IEnumerable&lt;Annotation&gt; annotations... 它在 IEnumerable&lt;Annotation&gt; annotations 的每次迭代中重新创建一个新的 Annotation 对象。

【问题讨论】:

  • 如何比较注释?完全相同的 Annotation 可以是内存中的不同对象。

标签: c# wpf linq oxyplot


【解决方案1】:

与您之前添加到MyOxyPlotModel.Annotations 的实例相比,您可能将不同的Annotation 实例传递给您的RemoveAnnotation 方法。将Annotations 中不存在的实例传递给Annotations.Remove 不会删除任何内容,因为无法确定要删除的注释。

确保在 AddAnnotationRemoveAnnotation 方法中使用相同的实例,或者使用注释的属性将其与现有的进行比较。

例如,如果您使用派生自 TextualAnnotation 的注解,您可以通过 Text 属性来比较它们。像这样的:

public void RemoveAnnotation(IEnumerable<Annotation> annotations)
{
    foreach (var annotation in annotations)
    {
        if (MyOxyPlotModel.Annotations.Contains(annotation))
            MyOxyPlotModel.Annotations.Remove(annotation);
        else if (annotation is TextualAnnotation ta)
        {
            var existingTa = MyOxyPlotModel.Annotations.OfType<TextualAnnotation>().FirstOrDefault(x => x.Text == ta.Text);
            if (existingTa != null)
                MyOxyPlotModel.Annotations.Remove(existingTa);
        }
    }

    RefreshAxisSeriesPlot();
}

【讨论】:

  • 谢谢!你让我走上正轨。事实上,我还没有完成对我的 LINQ 查询的评估,我从中得到了我的 IEnumerable&lt;Annotation&gt; annotations... 它在 IEnumerable&lt;Annotation&gt; annotations 的每次迭代中重新创建一个新的 Annotation 对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
相关资源
最近更新 更多