Under review

PlotModel not available for several plot controls

Oystein Bjorke 6 years ago updated by Jean Michel Decoret 6 years ago 2
This discussion was imported from CodePlex

Patoon wrote at 2012-07-12 12:17:


Firstly, congratulations and thank you for this super project. and thank you for sharing that !

I have a question about the Plot class in OxyPlot.WPF namespace.

Due to InvalidOperationException ("This PlotModel is already in use by some other plot control.") ,

,it is not possible for several plotControls to have the same PlotModel. Is there a reason to this choice ?

I thought that could be interesting to have the possibility to represent several plotControl to different location of an application, all bound on the same PlotModel.

In this case, the code in the OnModelChange() method would be :

 if (this.Model != null)
                    if (this.Model.PlotControl == null)                    
                        this.currentModel = this.Model;

What do you think about that ?

Thank you very much.

objo wrote at 2012-07-12 12:59:

The PlotModel is a "ViewModel" for the Plot - currently a lot of view-specific data is stored in there(e.g. axis scaling, coordinates transformed to screen space) , and it cannot be used in more than one view. I prefer to keep it this way (could of course also make the PlotModel contain only plot data, and a PlotViewModel containing the view specific data, but I think this would be more work...)

Patoon wrote at 2012-07-12 13:36:

Ok, I understand.

In this case, imagine the following scenario:

We have a UserControl hosting a PlotControl (with binding on a PlotModel), this UserControl DataContext is bound on a ViewModel called myVm (this ViewModel has so a PlotModel property)

For you, what would be the best way to reuse an other instance of this UserControl but bound on the same ViewModel instance (myVm) ?


objo wrote at 2012-07-13 09:40:

I would not reuse the same instance of the view model in different views. Can you create different instances?

Patoon wrote at 2012-07-13 10:50:

Indeed, even if my view model receive some modifications before display the new userControl, I think this is the most reasonable solution.

Thank you.

mklotz wrote at 2012-07-24 10:10:


I have a question that is directly related to this subject.
I have a viewmodel that contains an ObservableCollection of PlotModel. This collection is binded on the ItemsSource property of a ListView. For displayinf charts, I use a datatemplate. Here is my XAML :

 <ListView ItemsSource="{Binding PlotModels, Mode=OneWay}" ItemTemplate="{StaticResource ResourceKey=NamedCacheStatsTemplate}">
                    <WrapPanel Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" 
                                   ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}" 
                                   MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}" 
                                   ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
<DataTemplate x:Key="NamedCacheStatsTemplate" DataType="{x:Type oxymodel:PlotModel}">
        <oxy:Plot Model="{Binding}" MinHeight="300" MinWidth="600"/>

My viewmodel retrieves statistics from a webservice every 5 seconds, updates the existing plotmodels in the observablecollection of plotmodel, and call RaisePropertyChanged("PlotModels") for updating the UI. When the UI is updated, the exception InvalidOperationException ("This PlotModel is already in use by some other plot control.") is thrown.

Is there something wrong in my usage of OxyPlot, or, is there a workaround for this problem ?
Thanks in advance for your reply,

Best regards.



objo wrote at 2012-08-09 00:29:

I need a small test application to reproduce this bug. Can create it myself (will take some time) or maybe you can help? cheers!

isaacd1 wrote at 2012-11-04 09:04:


Firs I go with Patoon congratulations and thank...

I have the same problem

I use ListView with GroupBy option (using CollectionViewSource)

When I change the GroupBy (setting the CollectionViewSource.GroupDescriptions), WPF try to create a new control while the old one is still exist !


Best regards.

objo wrote at 2012-11-04 22:22:

Thanks for the bug report! Need a small test application to reproduce this bug.

isaacd1 wrote at 2012-11-05 08:23:

I reproduce the bug, how can I send it to you ?

objo wrote at 2012-11-06 19:43:

I suggest https://gist.github.com/ for single code files, or create a fork with the new example in Source\Examples\WPF

Is this the same bug as in http://oxyplot.codeplex.com/workitem/10000?

Otherwise we should create a new issue.

plandsberg wrote at 2012-11-27 15:27:

@isaacd1: Can you send your results, for analysing the problem? Thanks

zoman wrote at 2012-11-30 09:32:


It seems that only reloading the chart control causes this error. For instance, I use the Avalon Docking framework and whenever I switch from one tab to the other or float/dock a panel, I get this error. Is there any way to disable this check in the code? 

Thank you.

objo wrote at 2012-12-03 07:38:

Please test the latest version. I checked in a change Nov 23 related to loading/unloading the WPF control, I think this is the same problem as you encountered. I have tested the latest version with AvalonDock without problems.

jernejj wrote at 2013-03-12 23:56:


I'm getting the same error, but under rather strange conditions.

I have a class that holds certain data, among which is a PlotModel for an OxyPlot. I display objects of this class in a DataGrid, an excerpt of the code:
                                    <DataGridTextColumn Header="#" Width="Auto"/>
                                    <DataGridTemplateColumn Header="Image" Width="*">
                                                <Image Source="{Binding Path=DisplayImage}"></Image>

                                    <DataGridTemplateColumn Header="Chart" Width="*">
                                                <oxy:Plot Model="{Binding PM}" MinHeight="150">
When using the application, the user select certain parts of an image, those parts are analyzed and the data is displayed on a plot. The user is expected to select many regions of interest, so we get many plots, all listed neatly in a DataGrid.

Here's the interesting part. The problem only occurs if you scroll around, and only after a certain amount of time.

What's more is that it never occurs if the DataGrid is placed inside a ScrollViewer. So the workaround is obvious, I could just use ScrollViewer and be done with it, but it gives me other pains because the header is scrolled along with everything else. Any idea how to fix this?

jernejj wrote at 2013-03-20 14:45:

I hate to bump this, but this issue is really giving me some serious problems.

Anyone, please?

EDIT: obviously the problem is virtualization, if I turn it off for the DataGrid, the exception doesn't occur anymore. So reloading the control is still an issue?

willmoore88 wrote at 2013-04-10 11:56:

objo wrote:
I would not reuse the same instance of the view model in different views. Can you create different instances?
Hi. I'm trying this too, but even after I create an instance of my PlotModel, when I use this new instance with another plot, I get the same error.
Under review
I have added #175. Help wanted, I have no idea what is wrong... 
I find a workaround for this bug.

In my case the PlotView is used in a DataTemplate (In a Windows Style Metro App)

<HubSection Header="Graphic" HorizontalAlignment="Stretch" >
      <metro:PlotView Model="{Binding CurrentPlotModel}" MinWidth="1024" MinHeight="500" />

When my page is displayed the first time it works.
For the second display, in my viewmodel, I've updated the CurrentPlotModel , and  I have the bug.

CurrentPlotModel = new PlotModel {....}

The solution:

Before updating the CurrentPlotModel with the new value, I set it to null AND I call the garbage collector to clear correctly the plotview association.

CurrentPlotModel = null;<br>                    GC.Collect();<br>CurrentPlotModel = new PlotModel {....}

This is my only workaround. I've tested many many other cases, with no success.