Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "Hide()" a Modal WPF Window without it closing?

I have a WPF window that is run on a background thread as a sort of "notifier window"... when an event is raised, it displays a message... a user clicks the "Snooze" button and I call this.Visibility = Visibility.Collapsed

The very moment that I hide the window (either by calling this.Hide() or setting the Visibility as mentioned above)... the "ShowDialog()" code releases the window and closes it.

This is absolutely a bug in the WPF code (which I've identified via reflector)... but my question remains. Has anyone been able to come up with a work-around for this issue?

I've tried many things and am now reaching out to ya'll smart people :)

like image 893
Timothy Khouri Avatar asked Nov 30 '10 17:11

Timothy Khouri


People also ask

How do I hide the close button in WPF window?

Interop. WindowInteropHelper(this). Handle; SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU); } }... This is the code that removes the close button.

How remove maximize and minimize button in WPF?

In WPF you can indeed set the WindowStyle property of a Window to System. Windows. WindowStyle. ToolWindow to get rid of the minimize and maximize buttons completely, but the window will then look slightly different compared to when the property is set to its default value of SingleBorderWindow.

How do I close a window in WPF?

Pressing ALT + F4 . Pressing the Close button. Pressing ESC when a button has the IsCancel property set to true on a modal window.


2 Answers

You can't hide a modal dialog. That's like asking, "How do I get to 100mph in reverse?" You don't, you drive the car forwards.

Use Show, not ShowDialog. Alternately you can simply re-ShowDialog when it needs to become visible again.

like image 140
Tergiver Avatar answered Sep 22 '22 17:09

Tergiver


Timothy's Answer is good. I just needed for my scenerio to add the following

window.Closed += new EventHandler(window_Closed);
window.Show();
System.Windows.Threading.Dispatcher.Run(); 

and then in the event...

void window_Closed(object sender, EventArgs e)
{
    System.Windows.Threading.Dispatcher.ExitAllFrames();
}

I needed to do this because it was hanging on the Run after the form was really closed.

like image 28
xer21 Avatar answered Sep 22 '22 17:09

xer21