Hiding rather than closing a System.Windows.Window - problem
Hi all,
I want to hide a System.Windows.Window when it is closed (via the [X] control or Alt-F4). I've seen a few different places say that the solution is to handle the Closing event with a CancelEventHandler as such:
private void MyWindowClosing(object sender, CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
However when I do this I get the error:
"Cannot set visibility or call Show, ShowDialog, Close or Hide while window is closing."
I am using .NET 3.0 and WPF. Am I doing something wrong or is there now a different way to achieve this?
Thanks very much,
Keiron
AlanPosted Jul 13, 2007, 6:54 PM
I've done very little so far with WPF but it does say in the documentation that (unlike Windows Forms) you can't hide a window when it's closing:
http://msdn2.microsoft.com/en-us/library/system.windows.window.closing.aspx
All I can think of is to try adding a second handler to the Closing event which hides the form. By then the Closing event will have been cancelled by the first handler and the new state of the Cancel property should be persisted within the CancelEventArgs object passed to the second handler.
However, I'm not at all sure that this will work as the window might still be regarded as 'closing' because that's the event that has invoked both handlers.
Jan MontanoPosted Jul 12, 2007, 9:15 PM
For .net 2.0, I used this:
private void Form1_Load(object sender, EventArgs e)
{
}
void form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
And it works fine.