Click here to Skip to main content
15,792,842 members
Please Sign up or sign in to vote.
2.20/5 (2 votes)
See more:
Hello,
I am desiging wpf application and I am trying to navigate from Login screen to Home Screen by this code...I am successful in that part... My problem is that after clicking Logout button in home screen I want to Show again Login Page... I am successful in that part also... But after coming again to login page and i the user tries to relogin I am getting an error as below...

Error :"
Specified element is already the logical child of another element. Disconnect it first.





C#
Views.Home winHome = new Views.Home();
 object tmpwinHome= null ;
 tmpwinHome= winHome.Content;
 winHome.Content = null;


 foreach (Window window in Application.Current.Windows)
 {
  if (window.GetType() == typeof(Login1))
    {
    (window as Login1).Content = new ContentControl() { Content = tmpwinHome};

     }
 }
Posted
Updated 5-Dec-13 3:14am
v2
Comments
Sergey Alexandrovich Kryukov 5-Dec-13 12:12pm    
In what line? Is it in "Content = tmpwinHome"?

Actually, this exception is the result of a well-known WPF feature which protects you from misuse of child-parent relationships. I mean it: a feature, not a problem. The message is self-explanatory: don't add a UI element to anything, if it is already added to some parent.

—SA
jing567 7-Dec-13 23:29pm    
Can u give me any sample snippet code so that I can implement in my apllication

1 solution

One explanation I read on-line is
Quote:
If element is the child of a Panel (e.g. Grid) you have to remove it from the Panel's Children collection. If it is set as Content of a ContentControl, you'd have to set that Content to null (or anything else that is not element).

Here is a link that might help you:
[WPF Error]

Some more of the same questions on-line:
[Specified element is already the logical child of another element]
[Specified element is already the logical child of another element. Disconnect it first.]
[Specified element is already the logical child of another element. Disconnect it first]


I hope that these links will help you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900