Specified element is already the logical child of another element. Disconnect it first."
在将一堆Frame
控件转换为更简单的ContentControl
控件以在其逻辑父模型视图中托管子模型视图之后,我最近开始遇到异常。
在下面的示例中,注释掉的代码将阻止崩溃的发生:
<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>
在搜索了这种异常之后,最常见的原因似乎是一些不正确的样式,但是在这个应用程序中,我还没有设置任何元素的样式。有人可以告诉我可能导致此异常的原因是什么吗?
谢谢,亚历克斯。