"The specified element is already a logical child of another element. Disable it first." Error on second window call

I am currently trying to fix the error when I load my window1 after logging into my application (this works fine), then I use the functionality I created to go back to the login form to change the logged in user and I get an error while loading my windows1 second time

Error message "The specified item is already a logical child of another item. Disable it first."

Window1 code

public Window1()
        {
            InitializeComponent();

            var tc = TabsControl.Instance as Control;
            Grid.SetColumn(tc, 1);
            Grid.SetRow(tc, 1);

            //error happens on this line
            grdMainContent.Children.Add(tc);
}

      

not sure how to disable this item and you had no problem with google.

I've tried the following things.

grdMainContent.Children.Remove(tc);

      

and

if (!grdMainContent.Children.Contains(TabsControl.Instance as Control))
            {
                 grdMainContent.Children.Add(tc);
            } 

      

and

if (grdMainContent.Children.Contains(tc))
            {
                grdMainContent.Children.Add(tc);
            }

      

Any help is always appreciated

+3


source to share


1 answer


This is because you seem to be reusing the TabsControl that was previously used and added as a child of grdMainContent to another instance of your window.

You can detach it with:



Grid parentGrid = tc.Parent as Grid;
If(parentGrid != null) {
    parentGrid.Children.Remove(tc);
}

      

+1


source







All Articles