title: WPF Specified element is already the logical child of another element. Disconnect it first
body: <p>I have recently started getting the exception "<code>Specified element is already the logical child of another element. Disconnect it first."</code> after converting a bunch of <code>Frame</code> controls to simpler <code>ContentControl</code> controls to host sub-model views within their logical parent model views.</p>
  
  <p>In the below example, the commented out code when commented out will stop the crash from occurring:</p>
  
  <pre><code>&lt;UserControl x:Class="GUI.Views.Scenario.PathogenRiskLiquidIngestionView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ScenModels="clr-namespace:GUI.ViewModels.ScenarioModels"&gt;
  
      &lt;StackPanel Orientation="Vertical"&gt;
          &lt;StackPanel Orientation="Vertical"&gt;
              &lt;DockPanel&gt;
                  &lt;Label DockPanel.Dock="Left" Content="Risk Assessment Title"&gt;&lt;/Label&gt;
                  &lt;TextBox DockPanel.Dock="Right" Text="{Binding RiskAssessmentTitle, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"&gt;&lt;/TextBox&gt;
              &lt;/DockPanel&gt;
              &lt;DockPanel&gt;
                  &lt;Label DockPanel.Dock="Left" Content="Risk Calculated"&gt;&lt;/Label&gt;
                  &lt;ComboBox SelectedItem="{Binding RiskCalculated, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding RiskCalculatedOptions}"&gt;&lt;/ComboBox&gt;
              &lt;/DockPanel&gt;
              &lt;DockPanel&gt;
                  &lt;Label DockPanel.Dock="Left" Content="{Binding MinAcceptableInfectionRiskLabel}"&gt;&lt;/Label&gt;
                  &lt;TextBox DockPanel.Dock="Right" Text="{Binding MinAcceptableInfectionRisk, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"&gt;&lt;/TextBox&gt;
              &lt;/DockPanel&gt;
          &lt;/StackPanel&gt;
          &lt;!--&lt;GroupBox Header="Pathogens"&gt;
              &lt;StackPanel Orientation="Vertical"&gt;
                  &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Right"&gt;
                      &lt;Button Content="Add Pathogen" Command="{Binding AddPathogen}"&gt;&lt;/Button&gt;
                      &lt;Button Content="Remove Pathogen" Command="{Binding RemovePathogen}" CommandParameter="{Binding SelectedIndex, ElementName=PathogenList}"&gt;&lt;/Button&gt;
                  &lt;/StackPanel&gt;
                  &lt;ListView Name="PathogenList" ItemsSource="{Binding PathogensPresentViews}" Tag="{Binding}" BorderThickness="0" Background="Transparent"&gt;
                      &lt;ListView.ItemTemplate&gt;
                          &lt;DataTemplate DataType="{x:Type ScenModels:PathogenPresentIngestViewModel}"&gt;
                              &lt;ContentControl Content="{Binding}"&gt;&lt;/ContentControl&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ListView.ItemTemplate&gt;
                  &lt;/ListView&gt;
              &lt;/StackPanel&gt;
          &lt;/GroupBox&gt;
          &lt;GroupBox Header="Receptor Characteristics"&gt;
              &lt;StackPanel Orientation="Vertical"&gt;
                  &lt;ContentControl Content="{Binding ReceptorVolumeLiquidIngestedPerEventView}"&gt;&lt;/ContentControl&gt;
                  &lt;ContentControl Content="{Binding ExposuresView}"&gt;&lt;/ContentControl&gt;
              &lt;/StackPanel&gt;
          &lt;/GroupBox&gt;--&gt;
      &lt;/StackPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>After doing a search for this kind of exception the most common cause seems to be some incorrect styling, however in this application I have not yet styled any elements. Can someone please tell me what is likely to be causing this exception?</p>
  
  <p>Thanks,
  Alex.</p>