I have recently started getting the exception “Specified element is already the logical child of another element. Disconnect it first." after converting a bunch of Frame controls to simpler ContentControl controls to host sub-model views within their logical parent model views.
In the below example, the commented out code when commented out will stop the crash from occurring:
<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">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical">
<DockPanel>
<Label DockPanel.Dock="Left" Content="Risk Assessment Title"></Label>
<TextBox DockPanel.Dock="Right" Text="{Binding RiskAssessmentTitle, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</DockPanel>
<DockPanel>
<Label DockPanel.Dock="Left" Content="Risk Calculated"></Label>
<ComboBox SelectedItem="{Binding RiskCalculated, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding RiskCalculatedOptions}"></ComboBox>
</DockPanel>
<DockPanel>
<Label DockPanel.Dock="Left" Content="{Binding MinAcceptableInfectionRiskLabel}"></Label>
<TextBox DockPanel.Dock="Right" Text="{Binding MinAcceptableInfectionRisk, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</DockPanel>
</StackPanel>
<!--<GroupBox Header="Pathogens">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Add Pathogen" Command="{Binding AddPathogen}"></Button>
<Button Content="Remove Pathogen" Command="{Binding RemovePathogen}" CommandParameter="{Binding SelectedIndex, ElementName=PathogenList}"></Button>
</StackPanel>
<ListView Name="PathogenList" ItemsSource="{Binding PathogensPresentViews}" Tag="{Binding}" BorderThickness="0" Background="Transparent">
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type ScenModels:PathogenPresentIngestViewModel}">
<ContentControl Content="{Binding}"></ContentControl>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</GroupBox>
<GroupBox Header="Receptor Characteristics">
<StackPanel Orientation="Vertical">
<ContentControl Content="{Binding ReceptorVolumeLiquidIngestedPerEventView}"></ContentControl>
<ContentControl Content="{Binding ExposuresView}"></ContentControl>
</StackPanel>
</GroupBox>-->
</StackPanel>
</UserControl>
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?
Thanks,
Alex.
Without more knowledge of what all the underlying objects are i can not do much more than guessing…
Here is something really fishy:
As mentioned in the comments you should never even have a situation where you bind to
*Views, the view model should not have references to views, but only view models. There is little point in following a model-view-separation pattern if you violate it like that.Anyway, as you bind to what presumably are views and hence UI-elements something undesirable might be happening here:
DataTemplateis created: View is added toContentControl-> Now a child of a controlListViewgoes “Well screw theDataTemplate, i can display this item as is.” (Because it is a view, which it should not be).ListViewadds item (the view, which is the child of theContentControl) to theItemsPanel.But that is probably not it, WPF should be smarter than that, so how about this: Multiple references to the same instance in the
PathogensPresentViewslist?Or the same view model is used in two places, creating two views, which both try to display the same list of UI-elements?
Whatever the actual case may be, you should not fight the symptoms but the illness, which most likely is that your view models contain views.