From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Wpf Graph Binding and defining Axes and Plots only in ViewModel Invalid OperationException

Solved!
Go to solution

Hi,

I am using MVVM pattern and want to create a user control which contains a graph control.

I want to bind my plots and axes as described in Wpf Graph Binding and defining Axes and Plots only in ViewModel.

Everything seems ok when there is a one user control on page.

When I want to put 2 user controls on a page GraphExtension class throws "Child is already registered with a IRenderableGraph'1 parent" exception on a synchronizer.SourceCollection = CollectionViewSource.GetDefaultView( e.NewValue ); line.

 

   // Retrieve existing synchronizer, or create a new one for the target graph. 
            var synchronizer = (CollectionViewSynchronizer)graph.GetValue( PlotsSynchronizerProperty ); 
            if( synchronizer == null ) { 
                synchronizer = new CollectionViewSynchronizer { TargetCollection = graph.Plots }; 
                graph.SetValue( PlotsSynchronizerProperty, synchronizer ); 
            } 

            // Synchronize graph's plots with new source. 
            synchronizer.SourceCollection = CollectionViewSource.GetDefaultView( e.NewValue ); 
        } 

 

How can I correct error ?

Thanks in advance

Hakan

0 Kudos
Message 1 of 3
(2,399 Views)
Solution
Accepted by topic author yrnhkn

The exception is raised because an individual plot or axis object can only appear in one graph (although configuration values, like renderers and ranges can be shared).

 

In other words, each user control needs to provide a unique set of plots for its own graph. This can be done on the model side (by exposing unique plots everywhere you use the PlotsSource attached property), or it can be done in the attached property itself (by setting a custom synchronizer.ItemConverter = item => /* create Plot from source value */ item converter callback).

~ Paul H
0 Kudos
Message 2 of 3
(2,376 Views)
Solution
Accepted by topic author yrnhkn

I changed my code as 

 

synchronizer.ItemConverter = item => new Plot(){Rendere = ((Plot)item).Renderer} and evertythings seems to OK.

 

Thank you for answer.

 

Best Regards,

Hakan.

0 Kudos
Message 3 of 3
(2,369 Views)