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?
<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>
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>; }
}
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);
}
}