Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to find a way to close parent form in wpf.

this code is work for C#
C#
private void button1_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
    frm2.Show();
    this.Hide();
}

private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Close();
}

but i need WPF.

Does anyone know?
Posted
Comments
Sergey Alexandrovich Kryukov 28-Jul-14 15:21pm    
Even with Forms, this code makes no sense.
—SA

Why?
Why not just:
C#
private void button1_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    Hide();
    frm2.ShowDialog();
    Close();
}
 
Share this answer
 
Comments
sajjad bararjanian 28-Jul-14 15:16pm    
Because CPU usage is must reduce
Sergey Alexandrovich Kryukov 28-Jul-14 15:20pm    
Irrelevant!
—SA
Sergey Alexandrovich Kryukov 28-Jul-14 15:20pm    
No. Sorry, but ShowDialog does not require Close.
—SA
OriginalGriff 28-Jul-14 15:23pm    
It does if you want to close Form1, as his original example code does...
Sergey Alexandrovich Kryukov 28-Jul-14 15:28pm    
Sorry, I misread what you are actually closing...
—SA
Why not reading original MSDN documentation?

All you need is this: http://msdn.microsoft.com/en-us/library/system.windows.window.close(v=vs.110).aspx[^].

See also:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.isvisible(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.window.show(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.window.hide(v=vs.110).aspx[^].

See the sub-section "Lifetime Management" here: http://msdn.microsoft.com/en-us/library/system.windows.window%28v=vs.110%29.aspx[^].

It does not matter from where it is called from, from a method of what window, or not a window (as soon as it is called from the same UI thread). Besides, there is no such thing as "parent" window. There is a main window, owned window…

—SA
 
Share this answer
 
Comments
sajjad bararjanian 28-Jul-14 15:42pm    
That's not useful.
Sergey Alexandrovich Kryukov 28-Jul-14 19:39pm    
No, this is actually all you need.
—SA
sajjad bararjanian 28-Jul-14 15:58pm    
Sorry, I have question : how we can close windows 1 and open windows 2 in wpf ?
Sergey Alexandrovich Kryukov 28-Jul-14 19:40pm    
Read my answer more thoroughly; and you will see how to do it. Or explain your concerns.
And please tell me: are you the one who voted 1 or 3 for the answer? Would be interesting...
—SA
I've tested it and it worked for me.
You might also try to give you the same result

Thanks

C#
private void Button_Click(object sender, RoutedEventArgs e)
{
     NewMain win=new NewMain();
     win.Owner = this;
     this.Visibility = Visibility.Hidden;
     win.ShowDialog();
     this.Close();
}
 
Share this answer
 

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