Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 在包裹面板中添加多个堆叠面板………;_.net_Wpf - Fatal编程技术网

.net 在包裹面板中添加多个堆叠面板………;

.net 在包裹面板中添加多个堆叠面板………;,.net,wpf,.net,Wpf,我有一个包裹 名称为“PageWrapPanel” 在我的WPF应用程序的主窗口中 现在,我尝试在该包装面板中添加一个堆栈面板五次。 StackPanel stkPnl = new StackPanel(); stkPnl.Width = 300; stkPnl.Height = 150; stkPnl.Background = Brushes.DarkKhaki; for (int i = 0;

我有一个包裹 名称为“PageWrapPanel” 在我的WPF应用程序的主窗口中

现在,我尝试在该包装面板中添加一个堆栈面板五次。

StackPanel stkPnl = new StackPanel();
            stkPnl.Width = 300;
            stkPnl.Height = 150;
            stkPnl.Background = Brushes.DarkKhaki;

            for (int i = 0; i < 5; i++)
            {
                PageWrapPanel.Children.Add(stkPnl);
            }
StackPanel stkPnl=new StackPanel();
stkPnl.宽度=300;
标准高度=150;
stkPnl.Background=brusks.DarkKhaki;
对于(int i=0;i<5;i++)
{
PageWrapPanel.Children.Add(stkPnl);
}
但它不起作用…。
出了什么问题?

不能在逻辑树的多个位置添加相同的元素。您需要添加五个相同的
StackPanel
s

for (int i = 0; i < 5; i++)
{
    StackPanel stkPnl = new StackPanel();
    stkPnl.Width = 300;
    stkPnl.Height = 150;
    stkPnl.Background = Brushes.DarkKhaki;
    PageWrapPanel.Children.Add(stkPnl);
}
for(int i=0;i<5;i++)
{
StackPanel stkPnl=新StackPanel();
stkPnl.宽度=300;
标准高度=150;
stkPnl.Background=brusks.DarkKhaki;
PageWrapPanel.Children.Add(stkPnl);
}

顺便说一句,从异常中的错误消息(指定的元素已经是另一个元素的逻辑子元素)中可以看出这一点,您完全应该提供该异常,而不是让人们猜测。

您不能在逻辑树中的多个位置添加同一元素。您需要添加五个相同的
StackPanel
s

for (int i = 0; i < 5; i++)
{
    StackPanel stkPnl = new StackPanel();
    stkPnl.Width = 300;
    stkPnl.Height = 150;
    stkPnl.Background = Brushes.DarkKhaki;
    PageWrapPanel.Children.Add(stkPnl);
}
for(int i=0;i<5;i++)
{
StackPanel stkPnl=新StackPanel();
stkPnl.宽度=300;
标准高度=150;
stkPnl.Background=brusks.DarkKhaki;
PageWrapPanel.Children.Add(stkPnl);
}

顺便说一句,从您得到的异常中的错误消息(“指定的元素已经是另一个元素的逻辑子元素”)中可以看出这一点,您完全应该提供该异常,而不是让人们猜测。

尝试将StackPanel的创建放在循环中

for (int i = 0; i < 5; i++)
{
    StackPanel stkPnl = new StackPanel(); 
    stkPnl.Width = 300;
    stkPnl.Height = 150;
    stkPnl.Background = Brushes.DarkKhaki;
    PageWrapPanel.Children.Add(stkPnl);
}
for(int i=0;i<5;i++)
{
StackPanel stkPnl=新StackPanel();
stkPnl.宽度=300;
标准高度=150;
stkPnl.Background=brusks.DarkKhaki;
PageWrapPanel.Children.Add(stkPnl);
}

否则,您多次尝试将相同的StackPanel放置在WrapPanel中,这就是错误的来源。

尝试将StackPanel的创建放入循环中

for (int i = 0; i < 5; i++)
{
    StackPanel stkPnl = new StackPanel(); 
    stkPnl.Width = 300;
    stkPnl.Height = 150;
    stkPnl.Background = Brushes.DarkKhaki;
    PageWrapPanel.Children.Add(stkPnl);
}
for(int i=0;i<5;i++)
{
StackPanel stkPnl=新StackPanel();
stkPnl.宽度=300;
标准高度=150;
stkPnl.Background=brusks.DarkKhaki;
PageWrapPanel.Children.Add(stkPnl);
}
否则,您多次尝试将相同的StackPanel放置在WrapPanel中,这就是错误的来源