在wrap面板中添加多个堆叠面板..........

  • 本文关键字:wrap 添加 .net wpf
  • 更新时间 :
  • 英文 :


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

现在我正在尝试在这个wrap面板中添加一个堆栈面板五次....

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

但它不工作....出了什么问题?

您不能在逻辑树的多个位置添加相同的元素。你需要添加5个相同的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);
}

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

尝试在循环中创建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);
}

否则,你已经尝试放置相同的StackPanel在WrapPanel不止一次,这就是你的错误来自。

最新更新