C# 如何删除或隐藏OxyPlot图上的注释?
如何删除或隐藏图表上的注释?我正在这样做,但不起作用:C# 如何删除或隐藏OxyPlot图上的注释?,c#,wpf,linq,oxyplot,C#,Wpf,Linq,Oxyplot,如何删除或隐藏图表上的注释?我正在这样做,但不起作用: public void AddAnnotation(IEnumerable<Annotation> annotations) { foreach (var annotation in annotations) { MyOxyPlotModel.Annotations.Add(annotation); } RefreshAxisSeriesPlot(); } public void RemoveAnno
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);
public void AddAnnotation(IEnumerable annotation)
{
foreach(注释中的var注释)
{
MyOxyPlotModel.Annotations.Add(annotation);
}
RefreshAxisSeriesPlot();
}
公共无效删除注释(IEnumerable注释)
{
foreach(注释中的var注释)
{
MyOxyPlotModel.Annotations.Remove(注释);
}
RefreshAxisSeriesPlot();
}
私有void RefreshAxisSeriesPlot()=>MyOxyPlotModel.InvalidatePlot(true);
在这段代码中,添加注释是有效的,但删除注释是无效的
编辑:
好的,我在代码中发现了问题。
事实上我还没有完成LINQ查询的评估,从中我得到了
IEnumerable注释
…它在每次迭代IEnumerable注释时都会重新创建一个新的注释
对象您可能正在将注释的不同实例传递给RemoveAnnotation
方法将其与先前添加到MyOxyPlotModel.Annotations
的实例进行比较。将Annotations
中不存在的实例传递给Annotations.Remove
不会删除任何内容,因为无法确定要删除的批注
确保在AddAnnotation
和removeanotation
方法中使用相同的实例,或者使用批注的属性将其与现有的实例进行比较
例如,如果使用从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();
}
public void RemoveAnnotation(IEnumerable注释)
{
foreach(注释中的var注释)
{
if(MyOxyPlotModel.Annotations.Contains(annotation))
MyOxyPlotModel.Annotations.Remove(注释);
else if(注释为TEXTUALANTA符号)
{
var existingTa=MyOxyPlotModel.Annotations.OfType().FirstOrDefault(x=>x.Text==ta.Text);
if(existingTa!=null)
MyOxyPlotModel.Annotations.Remove(现有TA);
}
}
RefreshAxisSeriesPlot();
}
如何比较注释?完全相同的注释可以是内存中的不同对象。谢谢!你让我走上了正轨。事实上,我还没有完成LINQ查询的评估,从中我得到了IEnumerable注释
…并且它在每次迭代IEnumerable注释
时都会重新创建一个新的注释
对象。