Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
2.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...