Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-open the closed window?

i've seen so many samples that in order to open closed window i should hide the window in closing event, but this is not fair for me if i close window in middle of my work and again open same window it showing me content where i left because i'm hiding window previously. So how can i start freshly my window after closed or hide the window.

currently i'm calling winload method which is to show fresh window after calling the show method of a hide window.

    private PurgeData data=new PurgeData();

private void MenuPurgeData_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                if (PurgeData == null)
                {
                    PurgeData = new PurgeData();
                    PurgeData.Show();
                }
                else
                {
                    PurgeData.WinLoad();
                    PurgeData.Show();                    
                }
            }

Thanks, @nagaraju.

like image 371
nag Avatar asked Jan 05 '12 11:01

nag


1 Answers

If you want the behaviour of hide/show, you should not call Window.Close() on the window at all, but hide it by calling Window.Hide(). If the user has closed it though and a close is unavoidable, you can try the following. Override OnClosing inside the Window and set e.Cancelled to true, then call .Hide(). This should allow the window to be hidden/shown even if the user closes the window.

// Disclaimer, untested! 
protected override void OnClosing(CancelEventArgs e)    
{        
    e.Cancel = true;  // cancels the window close    
    this.Hide();      // Programmatically hides the window
}

EDIT:

Ok I've now read your question properly ;-) So how can i start freshly my window after closed or hide the window.

When you re-show the window using the above, it will of course be the same instance as the one that was previously hidden, hence will have the same data and state. If you want completely new contents you need to create a new Window() and call Window.Show() on that. If you hide/show as above then you'll get the window back in exactly the same state before it was hidden.

Are you using the MVVM pattern in your WPF application? If so, you could try the following. By having all the data in your ViewModel and bound to by the View (ie: no business logic or data in the code behind of the Window), then you could invoke a method on the ViewModel to reset all data when the window is shown. Your bindings will refresh and the window state will be reset. Note this will only work if you have correctly followed MVVM and bound all elements on the main form to ViewModel properties (including sub controls).

Best regards,

like image 186
Dr. Andrew Burnett-Thompson Avatar answered Oct 05 '22 11:10

Dr. Andrew Burnett-Thompson