12

In .Net 4.0 the following code throws an InvalidOperationException with the message "Specified element is already the logical child of another element. Disconnect it first."

var parent = new System.Windows.Controls.ContentControl();
var child = new System.Windows.Controls.Button();

parent.Content = child;

var parent2 = new System.Windows.Controls.ContentControl();
parent2.Content = child;    // throws InvalidOperationException in .Net 4.0, not in 4.5

However, running this code on a machine with .Net 4.5 installed results in no exception being thrown. This appears to cause the visual tree to have some strange state which shows up as an incorrect UI.

Why no exception? The throw statement appears to be still present in .Net 4.5 FrameworkElement.AddLogicalChild. What would cause it to be not thrown?

I'm happy to accept that the behavior changed for a good reason, and I have to change my coding, however, as it stands, the silent fail with corrupt UI seems like a step backward from the strong exception when the explicit disconnection of a FrameworkElement from the logical tree was forgotton.

codekaizen
  • 26,990
  • 7
  • 84
  • 140
  • The big problem about that exception is that it gives no information on which element needs to be disconnected from which. Do you know a way to capture that information from the exception? I'm glad it dissappeared because it fixed my app in 4.5, but it's scary not to know why or hoe to fix it. – Hannish Sep 10 '13 at 12:06
  • @Hannish - If you ever find a way to know which element needs to be disconnected, please post it. I have the same issue. Developping with VS2012 targeting 4.0. But the app. doesn't break because of .net 4.5 but it breaks on workstation that are in 4.0 (cannot update because of Windows XP). It makes debugging almost impossible. – PiercingDegree Sep 12 '13 at 19:39
  • @PiercingDegree I was also developing in VS2012 targeting 4.0, but nonetheless the 4.5 installed in the computer seems to interfere. I got another "clean" computer with Vista and VS2010 and moved the project there, and A LOT of exceptions (like this and others) appeared. I suggest you get a computer WITHOUT .NET Framework 4.5, and to use VS2010 if you are targeting 4.0 – Hannish Sep 13 '13 at 19:19
  • @Hannish - Thanks for the info. I've been able to debug the .Net 4.0 app on the Workstation. This link as been life saving : http://netmatze.wordpress.com/2012/08/24/using-windbg-exe-and-sos-dll-to-debug-a-net-4-0-application/ – PiercingDegree Sep 13 '13 at 19:33
  • @Hannish of course since only one is allowed (pre 4.5) that is the one that needs to be disconnected. On the contrary is scarier now with whatever 4.5 does, as the OP says: "the silent fail with corrupt UI seems like a step backward from the strong exception". – markmnl Dec 19 '13 at 07:29
  • @markmnl in 4.5 is not a silent fail, it actually works as I expected: you can assign another child without problems, and when you do the displayed view changes like a charm. When they say that the parent can have only one child, what I understand is that it cannot be a collection object, but it's not implied that the only child can never change. – Hannish Dec 19 '13 at 15:42
  • @Hannish I think you mis-understood: _a child cannot have more than one parent_ at the same time irrespective of whether that parent is Content or Items type – markmnl Dec 20 '13 at 00:11

1 Answers1

3

There are times when it makes sense for a child to have more than one logical parent, for instance in layout-to-layout animation. I'm guessing the WPF team decided it was time to let developers decide when and how to use this instead of disallowing it.

Nigel Shaw
  • 1,008
  • 1
  • 12
  • 16