Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

WPF graph:graph.Plots.Clear() error

Solved!
Go to solution

Hi,

I need to dynamically add plot to graph.The code as follows,

ObservableCollection<Point[]> dataSource = new ObservableCollection<Point[]>();

graph.DataSource = dataSource ;

public void AddOnePlot(Point[] pointData)

{

     Plot plot = new Plot();

     graph.Plots.Add(plot);

     dataSource.Add(pointData);

}

But when I call graph.Plots.Clear(),the error is"Range action are not supported.".How I clear all plots in graph?

Look forward to your reply.Thanks.

0 Kudos
Message 1 of 6
(6,680 Views)

I have found a solution.

 

for(int i = graph.Plots.Count;i>=0;i--)

{

    graph.Plots.RemoveAt(i);

}

This workaround is no problem.But for the graph.Plots.Clear() function,I don't know why it wrong.

Still hope your answer!

0 Kudos
Message 2 of 6
(6,665 Views)

I have found a solution.

 

for(int i = graph.Plots.Count;i>=0;i--)

{

    graph.Plots.RemoveAt(i);

}

This workaround is no problem.But for the graph.Plots.Clear() function,I don't know why it wrong.

Still hope your answer!

0 Kudos
Message 3 of 6
(6,665 Views)
Solution
Accepted by topic author lizi

The Plots collection on the Graph class derives from NotifyingCollection, which raises a single optimized aggregate event when multiple items change. The .NET ObservableCollection on the other hand raises separate events for every modified item in the collection. The Measurement Studio controls support both models, but WPF controls like ItemsControl do not support aggregate events (the "range actions" in the error message).


However, there is a bug in the current version of the Legend control: if you bind the ItemsSource property to a collection instead of to a graph, you may see this error if a default collection view gets created. To prevent this, you can serialize events (as you showed above, removing individual items instead of clearing), or you can use the NotifyCollectionChangedSimplifier to adapt the optimized collection to the "separate event for every item" model:


    xmlns:ni="http://schemas.ni.com/controls/2009/xaml/presentation"
    xmlns:niPrimitives="http://schemas.ni.com/controls/2009/xaml/presentation/primitives"

    ...

    <Grid.Resources>
        <niPrimitives:NotifyCollectionChangedSimplifier x:Key="CollectionSimplifier" />
    </Grid.Resources>

    ...

    <ni:Legend ItemsSource="{Binding Plots, ElementName=graph, Converter={StaticResource CollectionSimplifier}}" />

~ Paul H
0 Kudos
Message 4 of 6
(6,641 Views)

Thank you.It is very helpful to me.

0 Kudos
Message 5 of 6
(6,636 Views)

Just wanted to let you know this issue was fixed in the Measurement Studio 2015 release.

~ Paul H
0 Kudos
Message 6 of 6
(5,273 Views)