Reputation: 81
I am currently trying to fix a bug where i load my window1 after logging into my application (which works fine), then i use functionality i created to go back to the login form to change the logged on user and i get an error when loading my Window1 for the second time
Error message "Specified element is already the logical child of another element. Disconnect it first."
window1 code is
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 disconnect this element, and have sofar been unsuccessful on google.
what i have tried are 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 as always greatly appreciated
Upvotes: 3
Views: 3395
Reputation: 2521
It happens because you seem to reuse TabsControl that was previously used and added as a Children of grdMainContent in another instance of your window.
You can detach it with:
Grid parentGrid = tc.Parent as Grid;
If(parentGrid != null) {
parentGrid.Children.Remove(tc);
}
Upvotes: 1