Home > database >  UserControl: This PlotModel is already in use by some other PlotView control
UserControl: This PlotModel is already in use by some other PlotView control

Time:02-17

I know this question has been posted already, but I don't understand the answers. In My case I have a project with only a UserControl:

<UserControl x:Class="SimpleOxyPlotUserControl.UserControl1View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:SimpleOxyPlotUserControl"
             xmlns:oxy="http://oxyplot.org/wpf"
             mc:Ignorable="d" 
             x:Name="uc1"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <oxy:PlotView Model="{Binding ElementName=uc1, Path=OxyPlotModel, Mode=TwoWay}"/>
    </Grid>
</UserControl>

and its code behind:

using System.Windows;
using System.Windows.Controls;
using OxyPlot;

namespace SimpleOxyPlotUserControl
{
    /// <summary>
    /// Interaction logic for UserControl1View.xaml
    /// </summary>
    public partial class UserControl1View : UserControl
    {
        public UserControl1View() => InitializeComponent();

        public PlotModel OxyPlotModel
        {
            get { return (PlotModel)GetValue(OxyPlotModelProperty); }
            set { SetValue(OxyPlotModelProperty, value); }
        }

        public static readonly DependencyProperty OxyPlotModelProperty =
            DependencyProperty.Register("OxyPlotModel", typeof(PlotModel), typeof(UserControl1View),
                new PropertyMetadata(new PlotModel()));
    }
}

Then I have another project with a simple WPF App and its MainWindow.xaml:

<Window x:Class="SimpleOxyPlotUserControlApp.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:simpleOxyPlotUserControl="clr-namespace:SimpleOxyPlotUserControl;assembly=SimpleOxyPlotUserControl"
        Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <simpleOxyPlotUserControl:UserControl1View/>
        <simpleOxyPlotUserControl:UserControl1View Grid.Row="1"/>
    </Grid>
</Window>

and its code behind:

using System.Windows;

namespace SimpleOxyPlotUserControlApp.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

I receive the error message "InvalidOperationException: This PlotModel is already in use by some other PlotView control."

I know this is, because each Oxy PlotModel can only be connected to exactly one Oxy PlotView. But why does it not create a new Oxy PlotView and PlotModel each time, when I insert my UserControl? And how can I ensure that it does so?

CodePudding user response:

OxyPlotModelProperty is registered with new PlotModel() default value in PropertyMetadata. but OxyPlotModelProperty is static so you get the same PlotModel instance for all UserControl1View instances.

fix it by creating PlotModel in constructor:

public partial class UserControl1View : UserControl
{
    public UserControl1View() 
    {
        InitializeComponent();
        SetLocalValue(OxyPlotModelProperty, new PlotModel());
    }

    public PlotModel OxyPlotModel
    {
        get { return (PlotModel)GetValue(OxyPlotModelProperty); }
        set { SetValue(OxyPlotModelProperty, value); }
    }

    public static readonly DependencyProperty OxyPlotModelProperty =
        DependencyProperty.Register("OxyPlotModel", typeof(PlotModel), typeof(UserControl1View),
            new PropertyMetadata(null));
}
  • Related