Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

wpf - Specified element is already the logical child of another element. Disconnect it first”

i have user control that contain grid and inside it there is expander the problem is when i try to open new window that uses this user control i get an exception " Specified element is already the logical child of another element. Disconnect it first"
here is my code and its work when first windows created and the exception occur when show the second window

 <UserControl x:Class="DiagramDesigner.WindowsUserControl"
                     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:s="clr-namespace:DiagramDesigner"
                xmlns:c="clr-namespace:DiagramDesigner.Controls"
                     mc:Ignorable="d" 

                     d:DesignHeight="700" d:DesignWidth="1000">
            <UserControl.Resources>
                <ContextMenu x:Key="DesignerCanvasContextMenu">
                    <MenuItem Header="Paste" Command="{x:Static ApplicationCommands.Paste}">
                        <MenuItem.Icon>
                            <Image Source="Resources/Images/Paste.png" Width="16"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Header="Select All" Command="{x:Static s:DesignerCanvas.SelectAll}"/>
                </ContextMenu>
            </UserControl.Resources>

            <Grid Margin="10">
                <Grid.RowDefinitions>

                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>


                <Grid Grid.Row="1" Margin="0,10,0,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="200"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <!-- Toolbox -->
                    <StackPanel Grid.Column="0" Margin="0,0,5,0">
                        <Expander Header="Flow Chart" Content="{StaticResource FlowChartStencils}" IsExpanded="True" />
                    </StackPanel>
                    <!-- GridSplitter -->
                    <GridSplitter Focusable="False" Width="2" Background="LightGray"
                            VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
                    <!-- Designer -->
                    <GroupBox Header="Diagram" Grid.Column="1" Margin="3,0,0,0">
                        <ScrollViewer HorizontalScrollBarVisibility="Auto"
                              VerticalScrollBarVisibility="Auto">
                            <s:DesignerCanvas Focusable="true" x:Name="MyDesigner"
                                    Background="{StaticResource WindowBackgroundBrush}"
                                    Margin="10" FocusVisualStyle="{x:Null}"
                                    ContextMenu="{StaticResource DesignerCanvasContextMenu}"/>
                        </ScrollViewer>
                    </GroupBox>
                </Grid>
            </Grid>
       </UserControl>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot reuse the same UIElement instance for different parts of the UI. Either remove it from the first and add it to the second one (not recommended but there are some advanced scenarios were it makes sense) or simply create a new one. It's pretty cheap.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...