5

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.

nag
  • 920
  • 6
  • 27
  • 51

3 Answers3

11

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,

Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
  • currently i'm calling Load method when at the time of opening the hide window which is called at window_load method then it shows to me like fresh window. – nag Jan 05 '12 at 12:23
  • @nag, this doesn't sound like the right way to do things. You shouldn't specifically call Window_Load. the only methods you should call from external code are new() (to create a new instance), Close() (to close and never re-open), Show() (to show after new, or previous Hide()) and Hide() to hide the shown window. Best regards, – Dr. Andrew Burnett-Thompson Jan 05 '12 at 12:25
  • yeah! in order to show fresh window i'm calling a method that displays window as fresh, this is calling before .show() method calls of a hide window. Is this approach is right or wrong tell me? – nag Jan 05 '12 at 12:31
  • @nag, hi there. I suggest you post a cut-down version of your Window code, as its impossible to tell exactly what you mean without seeing the code. Best regards, – Dr. Andrew Burnett-Thompson Jan 05 '12 at 12:39
  • From my sample purgedata is a window calling from main window. – nag Jan 05 '12 at 12:53
  • @nag, edit your question please with the updated code so the community can see it. Regards, – Dr. Andrew Burnett-Thompson Jan 05 '12 at 12:55
1

It really depends on the structure of your app. Since you're not maintaining state, you only need to save the actual type of window that was closed. Depending on what type of window you're using, you can assign it an ID (e.g. in its Tag property) so that it can be recognized later. You can then push this ID during the Closing event in a Stack. Then, if the user reopens it, pop the stack to get the ID and check what window that corresponds to. You can then reopen that window.

Of course, Stack is just one data structure and it may or may not be suitable for you. Using a Stack means that user can reopen all the past windows they closed, but maybe you might just want the one window instead.

Edit - basic code:

//create an enum to make it easy to recognise the type of window that was open
enum WindowType { Profile, Settings };

//create a stack to hold the list of past windows
Stack<WindowType> pastWindows = new Stack<WindowType>();

//give the window type a particular id 
profileWindow.Tag = WindowType.Profile;

//open the window
profileWindow.Show(); 

//in the closing event, if you want the user to be able to reopen this window, push it to the stack
 protected override void OnClosing(CancelEventArgs e) 
 { 
       pastWindows.Push(WindowType.Profile); //or whatever type it was
       base.OnClosing(e);       
 } 

//to reopen the last window
void ReopenLastWindow()
{
   WindowType lastType = pastWindows.Pop();
   switch(lastType)
   {
      case WindowType.Profile:   
           profileWindow.Show();
           break;
      case WindowType.Settings: 
           settingsWindow.Show();
           break;
      default:
           break;
   }
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • I've added code. It's completely untested and was typed directly into StackOverflow, so there may be compiler errors on some properties/methods, but it should give you a general idea. – keyboardP Jan 05 '12 at 12:30
0
 this.Opacity = 0 

to "close the window"

 this.Opacity = 1 

to "re-open it"

A remark concerning the Hide() method: from another class, the window will in fact be considered as closed and the code will continue after a ShowDialog() method usage. Using the "Opacity" property overrides the problem.

Javert
  • 248
  • 2
  • 7