Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi,
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?

XML
<UserControl
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	mc:Ignorable="d"
	x:Class="Win8Panel.LeftRightPanel"
	x:Name="UserControl"
	d:DesignWidth="150" d:DesignHeight="500">
	<UserControl.Resources>
		<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" />
			</DoubleAnimationUsingKeyFrames>
		</Storyboard>
		<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"/>
			</DoubleAnimationUsingKeyFrames>
		</Storyboard>
	</UserControl.Resources>
	<UserControl.Triggers>
		<EventTrigger RoutedEvent="Mouse.MouseEnter">
			<BeginStoryboard x:Name="OnMouseEnter1_BeginStoryboard" Storyboard="{StaticResource OnMouseEnter1}"/>
		</EventTrigger>
		<EventTrigger RoutedEvent="Mouse.MouseLeave">
			<BeginStoryboard x:Name="OnMouseLeave1_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave1}"/>
		</EventTrigger>
	</UserControl.Triggers>


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

</UserControl>





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




C#
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;
        ContainerStk.Children.Add(stk);


    }
}
Posted
Updated 1-Sep-12 20:52pm
v3

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
 
Comments
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