locked
Specified element is already the logical child of another element. Disconnect it first. RRS feed

  • Question

  • Hi All,

    I have a toolbar, labels and text boxes in the main window. On a 'mousedown' event, I try to create a grid and add it to the existing window. When I do like  'this.Content = mygrid' , it shows the error message 'Specified element is already the logical child of another element. Disconnect it first'. Could you please help me to solve this issue.

    Thanks in advance

    Jo

    Tuesday, August 16, 2011 2:58 PM

Answers

  • Hi Michael and Tim,

    Thanks very much for your cooperation. Its really appreciated. I am using the Dockpanel to solve the issue. I am a beginner in Visual Studio applications.

    Thanks

    Jo

    • Marked as answer by s251984 Wednesday, August 17, 2011 3:36 PM
    Wednesday, August 17, 2011 3:35 PM

All replies

  • Your grid is already a child of another element.  You must remove it from that element first to add it as a child of another element.  I would have to see your code to help further.
    Michael Stacie
    Tuesday, August 16, 2011 4:15 PM
  • Hi Michael,

    This is the code I wrote in my .cs file

                Label homeLabel = new Label();
                homeLabel.Name = "HomeLabel";
                homeLabel.Content = "This is the home label";

                Grid homeGrid = new Grid();
                homeGrid.Height = 100;
                homeGrid.Width = 200;
                homeGrid.HorizontalAlignment = HorizontalAlignment.Left;
                homeGrid.VerticalAlignment = VerticalAlignment.Center;

                ColumnDefinition col1 = new ColumnDefinition();
                homeGrid.ColumnDefinitions.Add(col1);
                RowDefinition row1 = new RowDefinition();
                homeGrid.RowDefinitions.Add(row1);
                homeGrid.Children.Add(homeLabel);
                this.RemoveLogicalChild(toolBar1);
                this.Content = homeGrid;
                this.Content = toolBar1;

     

    The last line, toolBar1 is already exists and I want to add this grid into it. But the last line throws the exception.

    Tuesday, August 16, 2011 4:27 PM
  • I don't see a problem here, if I add a toolbar in XAML and run this code it loads just fine.
    Michael Stacie
    Tuesday, August 16, 2011 4:35 PM
  • You didn't add the grid to the toolbar, you added it to "this" which, I assume, is the window. In any case, I think what you are suggesting is that you want to add the toolbar to the grid, then set the content of the window to the grid?

    If I understand correctly, what you want to do is this:

    this.RemoveLogicalChild(toolBar1);
    homeGrid.Children.Add(toolBar1);
    this.Content = homeGrid;
    



    Check out My Blog for tech news, development tips, and other information for geeks like me.
    Tuesday, August 16, 2011 4:58 PM
  • Hi,

    I have a toolbar, labels and text boxes in the main window. And on 'Clicking' on a button in the toolbar, I want to show the grid in the same page along with the existing content. I dont want to add the toolbar into the grid. I just want to display the grid as well in the same page.

    Thanks

    Jo

    Tuesday, August 16, 2011 5:32 PM
  • Gotcha, a Window can have only one item as it's content, so the only way to show both the toolbar and the grid is to use a StackPanel/Grid/DockPanel and add the toolbar there along with the grid.


    Michael Stacie
    Tuesday, August 16, 2011 7:15 PM
  • Hi,

    Thanks very much for the reply. Unfortunately I am not able to add the toolbar to the grid by coding in .xaml.cs file. Could you please help me out!

    Jo

    Wednesday, August 17, 2011 10:39 AM
  • Not sure if this is exactly what you want but let me know:

       	  Grid homeGrid = new Grid();
     
                ColumnDefinition col1 = new ColumnDefinition { Width = GridLength.Auto};
                homeGrid.ColumnDefinitions.Add(col1);
                col1 = new ColumnDefinition();
                homeGrid.ColumnDefinitions.Add(col1);
     
                RowDefinition row1 = new RowDefinition { Height = GridLength.Auto };
                homeGrid.RowDefinitions.Add(row1);
                row1 = new RowDefinition();
                homeGrid.RowDefinitions.Add(row1);
     
                Label homeLabel = new Label();
                homeLabel.Name = "HomeLabel";
                homeLabel.Content = "This is the home label";
                Grid.SetColumn(homeLabel0);
                Grid.SetRow(homeLabel0);
                homeGrid.Children.Add(homeLabel);
     
                ToolBar toolBar = new ToolBar { Background=Brushes.Red};
     
                Grid.SetColumn(toolBar1);
                Grid.SetRow(toolBar0);
                homeGrid.Children.Add(toolBar);
     
                Border dummyContentBorder = new Border { Background = Brushes.Green };
                Grid.SetRow(dummyContentBorder1);
                Grid.SetColumnSpan(dummyContentBorder2);
                homeGrid.Children.Add(dummyContentBorder);
     
                this.Content = homeGrid;

    Michael Stacie
    Wednesday, August 17, 2011 1:40 PM
  • Hi Michael and Tim,

    Thanks very much for your cooperation. Its really appreciated. I am using the Dockpanel to solve the issue. I am a beginner in Visual Studio applications.

    Thanks

    Jo

    • Marked as answer by s251984 Wednesday, August 17, 2011 3:36 PM
    Wednesday, August 17, 2011 3:35 PM