title: Error This PlotModel is already in use by some other PlotView control in OxyPlot chart
body: <p>I am using Xamarin.Forms OxyPlot Chart. I have a collectionview and in each collectionview item has an expander and inside each of those expanders is a PlotView</p>
  <pre><code>&lt;CollectionView x:Name=&quot;Kids&quot;&gt;
      &lt;CollectionView.ItemTemplate&gt;
          &lt;DataTemplate&gt;
               &lt;xct:Expander Tapped=&quot;Expander_Tapped&quot; ClassId=&quot;{Binding rowNumber}&quot;&gt;
              &lt;xct:Expander.Header&gt;
                  &lt;Frame Padding=&quot;0&quot; CornerRadius=&quot;10&quot; Margin=&quot;5&quot; BackgroundColor=&quot;White&quot; HasShadow=&quot;False&quot;&gt;
                      &lt;StackLayout&gt;
                          &lt;Grid BackgroundColor=&quot;#f8f8f8&quot;&gt;
                              &lt;StackLayout Padding=&quot;5&quot; Orientation=&quot;Horizontal&quot;&gt;
                                  &lt;Image x:Name=&quot;kidProfile&quot; Source=&quot;{Binding image}&quot; WidthRequest=&quot;75&quot; HeightRequest=&quot;75&quot; HorizontalOptions=&quot;Start&quot; Aspect=&quot;AspectFill&quot; /&gt;
                                  &lt;StackLayout Orientation=&quot;Vertical&quot;&gt;
                                      &lt;Label Text=&quot;{Binding first_name}&quot;&gt;&lt;/Label&gt;
                                      &lt;StackLayout Orientation=&quot;Horizontal&quot;&gt;
                                          &lt;Label Text=&quot;Grade: &quot; FontSize=&quot;Small&quot;&gt;&lt;/Label&gt;
                                          &lt;Label Text=&quot;{Binding grade}&quot; FontSize=&quot;Small&quot;&gt;&lt;/Label&gt;
                                      &lt;/StackLayout&gt;
                                  &lt;/StackLayout&gt;
                              &lt;/StackLayout&gt;
                              &lt;Image Margin=&quot;20&quot; HorizontalOptions=&quot;End&quot; Source=&quot;arrowDown.png&quot; HeightRequest=&quot;15&quot;&gt;&lt;/Image&gt;
                          &lt;/Grid&gt;
                      &lt;/StackLayout&gt;
                  &lt;/Frame&gt;
               &lt;/xct:Expander.Header&gt;
               &lt;oxy:PlotView Model=&quot;{Binding chart}&quot; HeightRequest=&quot;200&quot; WidthRequest=&quot;100&quot; /&gt;
          &lt;/DataTemplate&gt;
      &lt;/CollectionView.ItemTemplate&gt;
  &lt;/CollectionView&gt;
  </code></pre>
  <p>and I was assigning the PlotModel in my class</p>
  <pre><code>public class ReportsClass
  {
      public PlotModel chart
      {
          get
          {
              PlotModel model = new PlotModel();
  
              CategoryAxis xaxis = new CategoryAxis();
              xaxis.Position = AxisPosition.Bottom;
              xaxis.MajorGridlineStyle = LineStyle.None;
              xaxis.MinorGridlineStyle = LineStyle.None;
              xaxis.MinorTickSize = 0;
              xaxis.MajorTickSize = 0;
              xaxis.TextColor = OxyColors.Gray;
              xaxis.FontSize = 10.0;
              xaxis.Labels.Add(&quot;S&quot;);
              xaxis.Labels.Add(&quot;M&quot;);
              xaxis.Labels.Add(&quot;T&quot;);
              xaxis.Labels.Add(&quot;W&quot;);
              xaxis.Labels.Add(&quot;T&quot;);
              xaxis.Labels.Add(&quot;F&quot;);
              xaxis.Labels.Add(&quot;S&quot;);
              xaxis.GapWidth = 10.0;
              xaxis.IsPanEnabled = false;
              xaxis.IsZoomEnabled = false;
  
  
              LinearAxis yaxis = new LinearAxis();
              yaxis.Position = AxisPosition.Left;
              yaxis.MajorGridlineStyle = LineStyle.None;
              xaxis.MinorGridlineStyle = LineStyle.None;
              yaxis.MinorTickSize = 0;
              yaxis.MajorTickSize = 0;
              yaxis.TextColor = OxyColors.Gray;
              yaxis.FontSize = 10.0;
              yaxis.FontWeight = FontWeights.Bold;
              yaxis.IsZoomEnabled = false;
              yaxis.IsPanEnabled = false;
  
  
              ColumnSeries s2 = new ColumnSeries();
              s2.TextColor = OxyColors.White;
  
              s2.Items.Add(new ColumnItem
              {
                  Value = Sunday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = Monday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = Tuesday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = Wednesday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = Thursday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
  
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = Friday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = Saturday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
  
              model.Axes.Add(xaxis);
              model.Axes.Add(yaxis);
              model.Series.Add(s2);
              model.PlotAreaBorderColor = OxyColors.Transparent;
  
              return model;
          }
      }
      
  }
  </code></pre>
  <p>Now this works, but in the Expander when I expand an item the PlotView would not show, at all. So I changed my Class to use INotifyPropertyChanged</p>
  <pre><code>public class ReportsClass : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
  
      public PlotModel chart
      {
          get =&gt; _chart;
          set
          {
              _chart = value;
              if(_chart.PlotView == null &amp;&amp; value.PlotView == null)
              {
                  OnPropertyChanged(&quot;chart&quot;);
              }
  
          }
      }
      public PlotModel _chart;
  
      protected void OnPropertyChanged(string propertyName)
      {
          var handler = PropertyChanged;
          if (handler != null)
              if(this.chart.PlotView == null)
              {
                  handler(this, new PropertyChangedEventArgs(propertyName));
              }
      }
  }
  </code></pre>
  <p>And in my code behind I use the expander's tapped method to populate the PlotView:</p>
  <pre><code>void Expander_Tapped(System.Object sender, System.EventArgs e)
  {
      if(expander != null)
      {
          expander.IsExpanded = false;
      }
      
      expander = sender as Expander;
  
      int id = Convert.ToInt32(expander.ClassId);
  
      ReportsClass item = newKidList[id];
  
      Device.StartTimer(TimeSpan.FromSeconds(1), () =&gt;
      {
          if (item.chart == null)
          {
              PlotModel model = new PlotModel();
  
              CategoryAxis xaxis = new CategoryAxis();
              xaxis.Position = AxisPosition.Bottom;
              xaxis.MajorGridlineStyle = LineStyle.None;
              xaxis.MinorGridlineStyle = LineStyle.None;
              xaxis.MinorTickSize = 0;
              xaxis.MajorTickSize = 0;
              xaxis.TextColor = OxyColors.Gray;
              xaxis.FontSize = 10.0;
              xaxis.Labels.Add(&quot;S&quot;);
              xaxis.Labels.Add(&quot;M&quot;);
              xaxis.Labels.Add(&quot;T&quot;);
              xaxis.Labels.Add(&quot;W&quot;);
              xaxis.Labels.Add(&quot;T&quot;);
              xaxis.Labels.Add(&quot;F&quot;);
              xaxis.Labels.Add(&quot;S&quot;);
              xaxis.GapWidth = 10.0;
              xaxis.IsPanEnabled = false;
              xaxis.IsZoomEnabled = false;
  
  
              LinearAxis yaxis = new LinearAxis();
              yaxis.Position = AxisPosition.Left;
              yaxis.MajorGridlineStyle = LineStyle.None;
              xaxis.MinorGridlineStyle = LineStyle.None;
              yaxis.MinorTickSize = 0;
              yaxis.MajorTickSize = 0;
              yaxis.TextColor = OxyColors.Gray;
              yaxis.FontSize = 10.0;
              yaxis.FontWeight = FontWeights.Bold;
              yaxis.IsZoomEnabled = false;
              yaxis.IsPanEnabled = false;
  
  
              ColumnSeries s2 = new ColumnSeries();
              s2.TextColor = OxyColors.White;
  
              s2.Items.Add(new ColumnItem
              {
                  Value = item.Sunday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = item.Monday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = item.Tuesday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = item.Wednesday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = item.Thursday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
  
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = item.Friday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
              s2.Items.Add(new ColumnItem
              {
                  Value = item.Saturday,
                  Color = OxyColor.Parse(&quot;#02cc9d&quot;)
              });
  
              model.Axes.Add(xaxis);
              model.Axes.Add(yaxis);
              model.Series.Add(s2);
              model.PlotAreaBorderColor = OxyColors.Transparent;
  
              item.chart = model;
          }
  
          return false;
      });
  }
  </code></pre>
  <p>However, eventually I will get this error:</p>
  <p>This PlotModel is already in use by some other PlotView control</p>
  <p>Now I understand that there is a one-to-one relation between the PlotView and its PlotModel, which gives us the error, so I have tried to do a check to see if PlotModel has a PlotView, but I am still getting this error.</p>
  <p>I found this solution:</p>
  <blockquote>
  <p>If you set the parent View of your OxyPlot to a DataTemplate that
  creates a new OxyPlot each time then the OxyPlot cannot be cached. A
  new PlotModel and PlotView is created each time and this error is
  avoided (at least that seems to work for me, I am using CarouselView)</p>
  </blockquote>
  <p><a href="https://github.com/oxyplot/oxyplot-xamarin/issues/17" rel="nofollow noreferrer">https://github.com/oxyplot/oxyplot-xamarin/issues/17</a></p>
  <p>But I do not know how to do this for a collection view, any help would be much apperciated.</p>
  <p>I also found this:</p>
  <blockquote>
  <p>This is a very common issue of OxyPlot when it's used in MVVM. In
  OxyPlot, the view and the model are 1-to-1 mapped, when a second view
  is trying to bind the same PlotModel, you have the issue of &quot;PlotModel
  is already in use by some other PlotView control&quot;. On iOS, ListView's
  cell will be re-created when it is scrolling. In this case, there will
  be a newly created view trying to bind the same PlotModel, and then
  you have the issue. On Android, I guess you will have the same issue
  too. Try to put your phone from landscape to portrait, see if you have
  the same issue. In Android, the view will be re-created completely
  when the orientation is changed.</p>
  <p>A quick fix is to break the MVVM design here a little bit. Don't
  create the model in a separated place but create the model in the
  view. So whenever a view is re-created by iOS or Android, a new model
  is also re-created.</p>
  </blockquote>
  <p><a href="https://github.com/oxyplot/oxyplot-xamarin/issues/60" rel="nofollow noreferrer">https://github.com/oxyplot/oxyplot-xamarin/issues/60</a></p>
  <p>But I don't know how to apply this part:</p>
  <blockquote>
  <p>A quick fix is to break the MVVM design here a little bit. Don't
  create the model in a separated place but create the model in the
  view. So whenever a view is re-created by iOS or Android, a new model
  is also re-created.</p>
  </blockquote>