Using MVVM, I have an AnnotationCollection databound to the SciChartSurface.Annotations. Individual LineAnnotations are added to the collection as the application code runs.
If annotations are added while the chart is displayed, they are all drawn correctly.
However, if the chart page is reloaded (say, the user navigates to a different page and then returns to the chart page), the annotations do not always redraw. If the annotations have AnnotationCanvas set to default (AnnotationCanvas.AboveChart, I believe) they redraw correctly about 50% of the time. If the annotations’ AnnotationCanvas is set to AnnotationCanvas.XAxis, they never redraw.
All the annotations still exist in the ObservableCollection in all these cases, so they shouldn’t need to be re-created.
Is this a bug in SciChart or is there something I may be doing wrong with the annotations?
(There is only one X-Axis so no axisID is necessary.)
LineAnnotation lAnno = new LineAnnotation(); lAnno.CoordinateMode = AnnotationCoordinateMode.RelativeY; lAnno.Y1 = 0.0; lAnno.X1 = DataPoints; lAnno.Y2 = 1.0; lAnno.X2 = DataPoints; lAnno.YAxisId = "Counts"; lAnno.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 255, 255)); lAnno.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(96, 255, 0, 0)); lAnno.StrokeThickness = 2; lAnno.AnnotationCanvas = AnnotationCanvas.XAxis; Markers.Add(lAnno);
Annotations cannot be attached to more than one parent element (This is a WPF restriction where each FrameworkElement is expected to have one parent).
When you attach an Annotation to a SciChartSurface then Annotation.Parent will be set to the canvas on the SciChartSurface.
When you fire-up another chart and attach the same annotations to another chart, they will still be attached to the previous chart, hence the issue you are seeing.
My recommendation is to do one of the following three options:
If only one chart is shown at a time, when closing a chart you can call SciChartSurface.Annotations.Clear(). This will detach all Annotations from that SciChartSurface
Or, if multiple charts are shown, then you will need to recreate annotations on each chart surface created
2.a A good way to do this is to use an MVVM technique to create annotations and only manipulate annotation ViewModels. For more discussion on this topic, see here
Please login first to submit.