

HACK: Stop the thread if the application is about to close if ( || Public bool ShowHandlerDialog( string message) " Message", typeof( string), typeof(ModalDialog), public static readonly DependencyProperty MessageProperty = This enables animation, styling, binding, etc.

Using a DependencyProperty as the backing store for Message. Public partial class ModalDialog : UserControl If we just use some simple elements, it’s OK. Attention: If you would like to display elements in our dialog that have a lot of "animation" stuff on it, we will run into problems. If we choose a sleep time that is small enough, for the user it seems that the application is running fluently. When the dialog is shown, we start a loop in which we suspend the calling thread (typically the UI thread) for a short time, and we also advise the WPF dispatcher to process the messages (mouse click, movements, keystrokes, repaint, etc.) that occur in the sleeping time. So we have to use a little hack here (I’m really not a friend of hacks, but here, it’s absolutely necessary). This is because WPF relies on a single thread model, which means it is impossible to run UI elements in another thread different from the thread of our main window. If it is suspended, an interaction with our dialog will also be impossible because it is suspended, too. To achieve this, we would have to suspend the calling thread (typically the UI thread). We have the following problem: "blocking" or "modal" means that the instruction pointer of our execution should remain at its position as long as the dialog is shown, and when the dialog is closed, it should continue. But how is that done? That’s a little bit tricky (you could also say "hacky" Of course, it is still not clear how the "modal" or "blocking" behavior of the dialog is achieved. The XAML of this control is quite simple: we have the half transparent border that we use as our overlay, the TextBlock to display our message, and two Buttons for OK / Cancel. The functionality and design is outsourced in a User Control. I would like to present an alternative solution.Īs shown in the image, we would like to have a half transparent overlay with some kind of content (here, just for example, we use a simple text message, but we could also display other controls). This has the disadvantage that a completely new "Windows"-window will be created which is not part of our main application window (which again has several disadvantages that I don’t want to discuss here). What can you do if you would like to display a modal / blocking dialog in WPF? By default, there is the possibility to use the Window.ShowDialog() method. There is a newer article with improved functionality here on CodeProject:
