LineSeries visibility in wpf Crash if not visible

wiyosaya 3 years ago in Bugs • updated 3 years ago 1
I'm using a LineSeries declared in xaml. I am binding an ObservableCollection<DataPoint> to the ItemsSource property. Because of my design, I would like to set the Visibility property on the LineSeries to Collapsed. Then, when I click on a control, I would like to set the Visibility property to Visible via a Style or Binding.

This is all certainly easy enough to do, however, I have found that if the Visibility property is set to Collapsed, datapoints are added to the observable collection, then the Visibility property is set to Visible, a null reference exception is thrown from LineSeries.Render where the code tests actualPoints.Count. It would appear that adding points to a bound observable collection when the LineSeries is not visible, and then changing visibility does not properly update the internal data point collection.

If, however, the visibility property is set to Visible when the observable collection is changed, I can then set the visibility property to anything I want, and all works fine.

Is this a bug?

I can work around this by setting some sort of "AllSeriesInitialized" flag, that keeps all of the series visible until they are all initialized, but it seems that the currently exhibited behavior is not ideal, IMHO.

What I found out is that if you want to hide/show a series in oxyplot via the Visible property, then the following must happen and in this order:
Each bound series must be Visible to start.
The backing collection must be populated.
plotView.InvalidatePlot must be called.

Once this happens, then it is possible to show/hide any series in the plot using the Visibility property.