0

So I realize this is an often-asked about error, and I've read many results from google, but I still can't figure out how it applies to my situation.

The setup: I create a window to present views. I create an instance of the first view and set the window's Content property to the view. Currently each of my views is a window itself. This is working perfectly on 7/10 computers. However, for some reason on a few computers (verified correct .NET version), switching to the second view (creating an instance of the second view and setting the window's Content property to the second view) throws a System.InvalidOperationException Specified element is already the logical child of another element. Disconnect it first.

I read that using ContentTemplate instead of Content would solve the problem, but that just produces a blank screen instead of the second view (I'm sure I'm doing that wrong).

I've also read that you should use a UserControl instead of a Window as the Window's Content property, so I'm trying that (I don't have a computer that's getting this error, so I have to wait to hear back about success). But it seems to me that that won't really change anything.

Any ideas why this is only happening on some computers, and any ideas on how to fix it?

Edit: Changing the views to UserControls did not solve the issue

Edit: I realized that I misspoke. I don't set the Content of the main window to a Window, I set it to the second Window's Content. So like:

MainWindow.Content = nextScreen.Content;
Tevis
  • 729
  • 1
  • 12
  • 27
  • Given that the Window class overrides OnAncestorChanged and throws an exception if you try to parent it I'm not sure how you would be using a Window but if you want to find out who the current logical parent is you should just use LogicalTreeHelper.GetParent on the object you are trying to set as the Content. – AndrewS Jun 17 '13 at 01:54
  • LogicalTreeHelper.GetParent returns null, which is expected since I just instantiate the view and then assign it to the Window's Content – Tevis Jun 17 '13 at 02:09
  • Setting the Content of a ContentControl will make that object (assuming its a DependencyObject) be a logical child of that ContentControl. Window is a ContentControl so if the Content you are setting is a DO (e.g. an element) then the exception is correct. You would need to get the content of the 1st window into a local variable, set the Content of that Window to null and then set the Content of the second window to the local variable. – AndrewS Jun 17 '13 at 02:27
  • @AndrewS You should post these three lines of code as an answer. – Clemens Jun 17 '13 at 07:33

1 Answers1

0

Thanks for the answers everyone. I discovered that changing my views to UserControls was the correct path. When I tried it the first time I made this mistake:

window.Content = userControl.Content;

instead of

window.Content = userControl;

which still worked on my machine, but continued to fail on the computers that were previously having problems.

I still don't understand why only a handful of computers were throwing an exception over this. Obviously there is some difference between them, though it wasn't anything obvious like Windows version or processor architecture.

Tevis
  • 729
  • 1
  • 12
  • 27