【问题标题】:The element is already the child of another element该元素已经是另一个元素的子元素
【发布时间】:2014-08-07 13:48:16
【问题描述】:

我正在处理一个大型 Silverlight 项目,但我遇到了问题,所以我将问题分解为较小的问题,以便在 stackoverflow 上提问。

我在这段代码中有一个大网格 (LayoutRoot)。在这个 LayoutRoot 里面我有超过 2 行(我将只使用超过 2 行中的 2 行)。在第一行我将有组合框,在另一个 我将在组合 selectionChnged 事件上显示文本(在两个不同的行中),但请注意文本的显示必须在 for 循环中的两个不同行中 因为在更大的程序中,我有类似的情况,其中 for 循环中的每个 if 条件都会返回一个包含一些数据/UI 元素的网格,我将每个网格一个接一个地存储在不同的列中,然后再次将所有行存储在一个大网格。

它当然会在“storeRowGridInBig.Children.Add(rowGrid);”行第二次执行 for 循环时给出“已经有子异常” 但问题是现在如何解决? (我要做的是:在 for 循环中的每个 if 条件中,for 循环的每次迭代都会返回一个网格,这些网格是 应该是逐行显示)如何解决这个问题?

我的尝试是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication6
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            ComboBox cmb = new ComboBox();
            Grid largeGrid = new Grid();
            for (int i = 0; i <4; i++)
            {
                largeGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
                largeGrid.ColumnDefinitions.Add(new ColumnDefinition() { });
                cmb.Items.Add(i);
            }   

            cmb.SelectionChanged += (o, e) =>
                {
                    Grid rowGrid = new Grid();
                    Grid storeRowGridInBig = new Grid();
                    for (int i = 0; i < 4;i++ )
                        rowGrid.RowDefinitions.Add(new RowDefinition());

                    for (int i = 0; i < 4; i++)
                    {
                        if (i == 0)
                        {
                            TextBlock txt1 = new TextBlock();
                            txt1.Text = "for 1";
                            rowGrid.Children.Add(txt1);                                            
                        }
                        else if (i == 1)
                        {
                            TextBlock txt1 = new TextBlock();
                            txt1.Text = "for 2";
                            rowGrid.Children.Add(txt1);                          
                        }
                        Grid.SetRow(rowGrid, i);
                        storeRowGridInBig.Children.Add(rowGrid);     //on puting it outside it shows both the text in 1 line overwrites I dont know why ?)         
                    }                  
                    Grid.SetRow(storeRowGridInBig, 1);
                    largeGrid.Children.Add(storeRowGridInBig);
                };

            Grid.SetRow(cmb,0);
            Grid.SetColumn(cmb, 1);
            largeGrid.Children.Add(cmb);
            LayoutRoot.Children.Add(largeGrid);
        }
    }
}

还有:

<UserControl x:Class="SilverlightApplication6.MainPage"
    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"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">

    </Grid>
</UserControl>

如何解决目前的情况。

【问题讨论】:

    标签: c# .net silverlight grid children


    【解决方案1】:

    您收到异常是因为您在该行中多次将相同的控件添加到其父级:

    storeRowGridInBig.Children.Add(rowGrid);
    

    把它放在for循环之外,它应该可以工作

    但正确的方法是不在代码隐藏中添加任何控件

    看看MVVM设计模式

    更新:

    重写了你的方法,所以它应该可以工作,但它确实不是一个干净的解决方案:

      cmb.SelectionChanged += (o, e) =>
      {
        Grid rowGrid = new Grid();
        Grid storeRowGridInBig = new Grid();
        for (int i = 0; i < 4; i++)
          rowGrid.RowDefinitions.Add(new RowDefinition());
    
        for (int i = 0; i < 4; i++)
        {
          if (i == 0)
          {
            TextBlock txt1 = new TextBlock();
            txt1.Text = "for 1";
            rowGrid.Children.Add(txt1);
            Grid.SetRow(txt1, i);
          }
          else if (i == 1)
          {
            TextBlock txt1 = new TextBlock();
            txt1.Text = "for 2";
            rowGrid.Children.Add(txt1);
            Grid.SetRow(txt1, i);
          }
        }
    
        storeRowGridInBig.Children.Add(rowGrid);
    
        Grid.SetRow(storeRowGridInBig, 1);
        if (LayoutRoot.Children.Count > 1)
        {
          LayoutRoot.Children.RemoveAt(LayoutRoot.Children.Count - 1);
        }
        LayoutRoot.Children.Add(storeRowGridInBig);
      };
    


    有几个问题:

    • 您多次添加rowGrid
    • 您在rowGrid 上使用了Grid.SetRow 而不是txt1
    • SelectionChanged 再次被调用时,您在上一次运行中添加的网格没有被删除

    【讨论】:

    • 对不起,能详细解释一下吗?
    • 我只为 LayoutRoot 制作了 1 个孩子,其中包含 2 个网格,但仍然存在同样的问题。在我更大的项目中,我正在使用 MVVM。
    • 如果我把它放在 poutside 它会在同一行打印两个文本,插入 2 个不同的行。如果您不信任我,请检查我是否已更新完整代码。
    • 如果您也解释一下之前的问题是什么以及您如何消除问题,我将非常感谢?非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多