C# 该元素已经是另一个元素的子元素

C# 该元素已经是另一个元素的子元素,c#,.net,silverlight,grid,children,C#,.net,Silverlight,Grid,Children,我在一个大型silverlight项目中工作,在那里我遇到了一个问题,所以我将问题分解到较小的问题上,在stackoverflow上询问 我在这段代码中有一个大网格(LayoutRoot)。在这个LayoutRoot中,我有超过2行(我将使用超过2行中的2行)。在第一行,我将有组合框,在另一行 在combo SelectionChanged事件中,第i行将显示文本(在两个不同的行中),但请注意,在for循环中,文本的显示必须在两个不同的行中 b因为在更大的程序中,我有类似的情况,for循环中的每

我在一个大型silverlight项目中工作,在那里我遇到了一个问题,所以我将问题分解到较小的问题上,在stackoverflow上询问

我在这段代码中有一个大网格(LayoutRoot)。在这个LayoutRoot中,我有超过2行(我将使用超过2行中的2行)。在第一行,我将有组合框,在另一行 在combo SelectionChanged事件中,第i行将显示文本(在两个不同的行中),但请注意,在for循环中,文本的显示必须在两个不同的行中 b因为在更大的程序中,我有类似的情况,for循环中的每个if条件将重新构建一个包含一些数据/UI元素的网格,我将每个网格逐个存储在不同的列中,并再次将所有行存储在一个大网格中

当然,在第二次执行for循环时,它会在“storeRowGridInBig.children.Add(rowGrid);”行给出“已经有子对象异常” 但问题是现在如何解决?(我要做的是:在for循环中的each 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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Animation;
使用System.Windows.Shapes;
命名空间SilverlightApplication6
{
公共部分类主页面:UserControl
{
公共主页()
{
初始化组件();
组合框cmb=新组合框();
网格大网格=新网格();
对于(int i=0;i
{
网格行网格=新网格();
Grid storeRowGridInBig=新网格();
对于(int i=0;i<4;i++)
添加(新的RowDefinition());
对于(int i=0;i<4;i++)
{
如果(i==0)
{
TextBlock txt1=新的TextBlock();
txt1.Text=“对于1”;
rowGrid.Children.Add(txt1);
}
else如果(i==1)
{
TextBlock txt1=新的TextBlock();
txt1.Text=“用于2”;
rowGrid.Children.Add(txt1);
}
Grid.SetRow(rowGrid,i);
storeRowGridInBig.Children.Add(rowGrid);//将其放在外部时,会显示1行中的文本都被覆盖(我不知道为什么?)
}                  
Grid.SetRow(storeRowGridInBig,1);
largeGrid.Children.Add(storeRowGridInBig);
};
网格设置行(cmb,0);
网格设置列(cmb,1);
大网格。子对象。添加(cmb);
LayoutRoot.Children.Add(大网格);
}
}
}
以及:



如何解决当前情况。

由于您将同一控件多次添加到行中的其父控件,因此出现异常:

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);
  };
cmb.SelectionChanged+=(o,e)=>
{
网格行网格=新网格();
Grid storeRowGridInBig=新网格();
对于(int i=0;i<4;i++)
添加(新的RowDefinition());
对于(int i=0;i<4;i++)
{
如果(i==0)
{
TextBlock txt1=新的TextBlock();
txt1.Text=“对于1”;
rowGrid.Children.Add(txt1);
Grid.SetRow(txt1,i);
}
else如果(i==1)
{
TextBlock txt1=新的TextBlock();
txt1.Text=“用于2”;
rowGrid.Children.Add(txt1);
Grid.SetRow(txt1,i);
}
}
storeRowGridInBig.Children.Add(rowGrid);
Grid.SetRow(storeRowGridInBig,1);
如果(LayoutRoot.Children.Count>1)
{
LayoutRoot.Children.RemoveAt(LayoutRoot.Children.Count-1);
}
LayoutRoot.Children.Add(storeRowGridInBig);
};

有几个问题:

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

对不起,请您详细解释一下好吗?我只制作了一个包含两个网格的LayoutRoot子项,但仍然存在相同的问题。在我的更大的项目中,我使用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);
  };