0

I'm trying to convert my old windows forms code into wpf. Each of my windows stored the visibility state, size and position and restored this states on the next run of the application. I connect to the FormCloseQuery event where it was possible to get information about the closereason. This is not possible in WPF.

I can store the position and size in the WPF forms' closing event, but it's not possible to store the visibility state.

Is there an answer how this can be done?

Thanks Martin

msedi
  • 1,437
  • 15
  • 25
  • These questions can help you: http://stackoverflow.com/questions/11169103/how-to-save-wpf-ui-state http://stackoverflow.com/questions/3784477/c-sharp-approach-for-saving-user-settings-in-a-wpf-application?lq=1 – Felix Keil Jul 15 '14 at 10:23
  • @cellz, *how* do you think either of your linked questions would help with their problem regarding the `Visibility`, or did you not read this question? – Sheridan Jul 15 '14 at 10:43

1 Answers1

1

I can store the position and size in the WPF forms' closing event, but it's not possible to store the visibility state.

Poppycock! What's wrong with the Window.Visibility property?:

private void MainWindow_Closing(object sender, CancelEventArgs e)
{
    // this.Visibility is the current visibility state of the Window
}

Also, for your information, it is a better idea to handle the Window.Deactivated event to update the values each time the application goes out of focus, or the Window.Closed event to update upon closing. The Closing event is specifically for cancelling the close request.

Furthermore, it is also customary in WPF to store bool values and then data bind them to the Window.Visibility property using the BooleanToVisibilityConverter Class.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Hi Sheridan. Thank for the comment. You are right, I'm using the Closed event not the Closing. The problem is that Closed is called when the window is closed by the user manually or by the application when exiting the application. If it's closed manually I don't want to open the window. If it's closed by the application it should show up in the next step. The CloseReason in winforms helped me out here, but it's not possible to detect how a WPF window was closed. – msedi Jul 15 '14 at 12:49
  • *it's not possible to detect how a WPF window was closed*... anything is possible. – Sheridan Jul 15 '14 at 13:14