Thursday, August 6, 2009


I added UIElements whose value is maintained across the application level, inside the Stack panel container control which is in Page_1.xaml. After leaving from Page_1.xaml to Page_2.xaml and again navigating to Page_1.xaml caused me an error while populating the global content to the stack panel. The error I got is,

"Specified element is already the logical child of another element. Disconnect if first."

The error was due to, at the first time population of stack panel went happily


but at the second time when I tried to populate the global content to the stack panel, the previous instance of stack panel still holds the global content and prevented me to populate the same content further and caused an exception.

I tried lot many workarounds for populating the stack panel the second time and later found the solution to be too simple.

In Page_1.xaml I created Closing = "Window_Closing" event. This event will be fired when the current window is closed. In this event I cleared the stack panel


so no more global content will be there before I go to new window(Page_2.xaml). Now if I go back to the Page_1.xaml the population of stack panel will happen with no exception as the contents are already cleared.