Click here to Skip to main content
15,794,275 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In my user control i have main StackPanel and i want add ObservableCollection<stackpanel> az child of main StackPanel Programmatically
in CollectionChanched event. but get this error :
specified element is already the logical child of another element.Disconnect it frist
can help me to find the problem,please?

	d:DesignWidth="150" d:DesignHeight="500">
		<Storyboard x:Key="OnMouseEnter1">
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="UserControl">
				<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="0.4"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.8" Value="1" />
		<Storyboard x:Key="OnMouseLeave1">
			<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="UserControl">
				<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0.4"/>
				<EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="0"/>
		<EventTrigger RoutedEvent="Mouse.MouseEnter">
			<BeginStoryboard x:Name="OnMouseEnter1_BeginStoryboard" Storyboard="{StaticResource OnMouseEnter1}"/>
		<EventTrigger RoutedEvent="Mouse.MouseLeave">
			<BeginStoryboard x:Name="OnMouseLeave1_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave1}"/>

    <Border Name="root">
    <StackPanel Name="ContainerStk" Orientation="Vertical" >


public static readonly DependencyProperty ItemsPanelProperty = DependencyProperty.Register
     new PropertyMetadata(new ObservableCollection<StackPanel>()));
 public ObservableCollection<StackPanel> ItemsPanel
     set { SetValue(ItemsPanelProperty, value); }
     get { return GetValue(ItemsPanelProperty) as ObservableCollection<StackPanel>; }

void ItemsPanel_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)

    foreach (StackPanel stk in ItemsPanel)
        stk.MinHeight = 100;
        stk.MinWidth = 100;
        stk.Background = Brushes.White;

Updated 1-Sep-12 21:52pm

1 solution

The stackpanel already belongs to the itemspanel, you must remove it from there before you can add it to the containerstk.Children. My guess is that you get an exception at the line where you want to add it.
The error is actually quite correct.
Share this answer
loger21 2-Sep-12 8:30am    
yes.exception at the line where i want to add it.but i do not know how to remove it from itemspanel with out error.
Philip Stuyck 2-Sep-12 12:56pm    
your problem is twofold. In a foreach you have to make sure the list on which you iterate on does not change. So make a copy of it with toList or toArray whatever.
Then iterate over the copy and remove the panel from the itemspanel. The remove method works because it is an observable collection. Then after removing it from the collection adding it to a different elements children should work. But if the items are already part of some controls chidren, you must remove it first. That is the point.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900