Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi All,
I am very new to wpf.
In my application i have a startup window and from this we will be going to the Mainwindow. In this main window we have tab controls. Under each tab we have different set of controls.
Now in my ribbon menu i want to include a button that will close this Mainwindow and has to go back to my Startup window.
How can i do this??

Please sugget.

I have written the following code in my Mainwindow.xaml:

<ribbon:ribbongroup header="To Main Screen" xmlns:ribbon="#unknown"> <ribbon:ribbonbutton largeimagesource="Images\Go.ico">
Label="Go Back" Command="{Binding GoBack_L1}" />

I have implemented this function in the .cs file which is the datacontext to my startupWindow.
The function is getting called. I hvae created an object of my startup window and calling the function Close() for startupWindow. But its not working, the strtupWIndow is not closing. Please let me know the solution.
Posted
Comments
Sergey Alexandrovich Kryukov 17-Nov-14 1:50am    
How come your start-up window is not a main window. I suspect it is. And what you call Mainwindow is not main. Main window is the one Application.Run started with; it is normally the same as starting one.
The problem is not clear. What prevents your from closing a window? Important thing is: are you going to show the closed window again? Then hide it instead closing.
—SA
Info7410 17-Nov-14 1:57am    
My strtup window is my initial screen that will be called from Application.Run
From this i will go to my Mainwindow in which i have all my controls placed. Now if i want to go back to my initial screen i am including a button in my mainwindow. PLease let me know what code i have to write inorder to get this functionality. My requirement is whenever i click button in my Mainwindow menu, i have to close this mainwindow and show up my initial screen.

1 solution

To close window, use Window.Close:
http://msdn.microsoft.com/en-us/library/system.windows.window.close(v=vs.110).aspx[^].

Please see my comment to the question, where I explain that in some cases you need to hide a window instead of closing, to later show it again:
http://msdn.microsoft.com/en-us/library/system.windows.window.hide(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.showdialog(v=vs.110).aspx[^].

If you need to hide the window, the problem you may face is this: F4 or the click on the non-client window close control [x] will still close the window instead of hiding. You will need to prevent it by overriding Window.OnClosing (alternatively, you could handle the event Closing):
http://msdn.microsoft.com/en-us/library/system.windows.window.onclosing%28v=vs.110%29.aspx[^].

To prevent closing, you will need to cancel it by setting the property CancelEventArguments.Cancel (with the event arguments passed as a parameter to your overridden method) to true:
http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel(v=vs.110).aspx[^].

To hide the window, call Window.Hide from the event handler.

—SA
 
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