Click here to Skip to main content
15,880,891 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF Dialog / MessageBox Manager

Rate me:
Please Sign up or sign in to vote.
4.81/5 (31 votes)
17 Mar 2013CPOL2 min read 95.4K   7K   67   27
Creating and layering message, progress and wait dialogs in WPF

Introduction

In this article, I want to introduce a small dialog management framework in WPF. It provides several dialogs which are all configurable. In addition, it is possible to cascade / layer several dialogues. The framework consists of:

  • Simple Message Dialog
  • Wait Animation Dialog  
  • Progress Dialog 
  • Custom Content Dialog  
Image 1

Using the Code

The entry point in the framework is managed via an instance of DialogManager. It provides an API that acts as a factory for creating the dialogs. The DialogManager itself needs a ContentControl to interact with. When a dialog is shown up, the content of the provided ContentControl is removed completely and replaced with a rendered image of the previous content and the layered dialogues. When all dialogues are closed, the original content will be placed back in the ContentControl.

Here's a simple example on how the MessageDialog works:

C#
var dialogManager = new DialogManager(this, Dispatcher);
dialogManager
	.CreateMessageDialog("Test", "I'm a dialog", DialogMode.Ok)
	.Show();   

For the WaitDialog, it is possible to specify a worker. When the worker is finished, you can configure the dialog to close immediately or to display a "finished" message.

C#
var dialog = _dialogManager
    .CreateWaitDialog("Waiting and waiting...", "Finished", DialogMode.Ok);
// We do some asynchronous work for 2 secs...
dialog.Show(() => Thread.Sleep(2000));

In addition, you can use a ProgressDialog, that accepts a worker, too. You can then set the progress in the worker as you like it:

C#
var dialog = _dialogManager
    .CreateProgressDialog("Progressing...", "Finished", DialogMode.Ok);
// We do some asynchronous work for 2 secs...
dialog.Show(() =>
    {
        for (var i = 0; i < 10; i++)
        {
            Thread.Sleep(200);
            dialog.Progress += 10;
        }
    });

Of course, you can place any WPF control inside the dialog by using the CustomContentDialog. In that case, it could be interesting to set the VerticalDialogAlignment and the HorizontalDialogAlignment properties of the dialog so that your content is scaled correctly.

Points of Interest

There are several configuration possibilities that you can see in the IDialog interfaces. By default, a click on one of the specified buttons (Ok, Cancel, etc.) closes the dialog. This behavior can be overwritten, and in addition, callbacks can be specified.

Thread safety: Creation of dialogs and showing them is automatically done in the UI thread so no dispatcher invoke is necessary.  

TODO's

It would be nice to provide a fluent API that brings more convenience to the user. That could be achieved by adding a configuration layer that will be accessible from the DialogManager. So, instead of using the factory methods, a small configuration model would be built up on every creation request that internally would interact with a dialog. At the end, some sort of "create" method could be called to commit the configuration and return a new dialog. The IDialogConfig interface demonstrates this approach, but I didn't have time to implement that so far.

Example:

C#
dialogManager
	.CreateMessageDialog("This is the message")
	.Caption("My Title")
	.Ok(() =>
		{
			// do something here...
		})
	.OkText("Commit")
	.CreateDialog()
	.Show(); 

History

  • v1 on 2013-03-17

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) www.technewlogic.de
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMemory leak Pin
Sts_ sterg17-Jan-21 11:15
Sts_ sterg17-Jan-21 11:15 
GeneralMy vote of 5 Pin
Hyland Computer Systems30-May-20 6:22
Hyland Computer Systems30-May-20 6:22 
QuestionThank You! Pin
Phillip Givens30-Jul-17 5:56
Phillip Givens30-Jul-17 5:56 
QuestionMain Window Does not re/render or draw when over lay is present Pin
Mallikarjun Kalkere8-Jun-17 9:15
Mallikarjun Kalkere8-Jun-17 9:15 
GeneralChange to Trim the Window Caption Bar Pin
DevKnightlie11-May-17 0:44
DevKnightlie11-May-17 0:44 
GeneralRe: Change to Trim the Window Caption Bar Pin
Phillip Givens30-Jul-17 5:54
Phillip Givens30-Jul-17 5:54 
QuestionWPF Dialog / MessageBox Manager - DialogResult.None (CreateWaitDialog) Pin
Member 1021551314-May-16 6:42
Member 1021551314-May-16 6:42 
AnswerRe: WPF Dialog / MessageBox Manager - DialogResult.None (CreateWaitDialog) Pin
Member 1021551314-May-16 7:48
Member 1021551314-May-16 7:48 
QuestionParent content is state from initialization Pin
FueledByIgnorance17-Aug-15 16:10
FueledByIgnorance17-Aug-15 16:10 
GeneralMy vote of 5 Pin
Jagd333-Jun-15 7:40
Jagd333-Jun-15 7:40 
QuestionGreat Control - One Issue Pin
Nemanja Cvetkovic27-Feb-15 3:02
Nemanja Cvetkovic27-Feb-15 3:02 
Questionvery usefull Pin
JasRaj Bishnoi12-Feb-15 1:17
JasRaj Bishnoi12-Feb-15 1:17 
GeneralMy vote of 3 Pin
nil_bond_0077-Nov-14 0:07
nil_bond_0077-Nov-14 0:07 
QuestionResize Windows while dialog is "open" Pin
Member 1038187619-Jun-14 4:00
Member 1038187619-Jun-14 4:00 
QuestionWPF Dialog / MessageBox Manager in a Non ViewModels system Pin
Daniel Brito2-Apr-14 8:25
Daniel Brito2-Apr-14 8:25 
AnswerRe: WPF Dialog / MessageBox Manager in a Non ViewModels system Pin
MWill 921-May-14 11:36
MWill 921-May-14 11:36 
QuestionGetting DialogResult not working Pin
carcabot30-Oct-13 0:47
carcabot30-Oct-13 0:47 
AnswerRe: Getting DialogResult not working Pin
Ronald Schlenker31-Oct-13 2:56
Ronald Schlenker31-Oct-13 2:56 
GeneralRe: Getting DialogResult not working Pin
carcabot31-Oct-13 9:14
carcabot31-Oct-13 9:14 
Questionclose dialog wiht buttons from the custom content. Pin
GeekByChoiCe20-Oct-13 18:19
GeekByChoiCe20-Oct-13 18:19 
AnswerRe: close dialog wiht buttons from the custom content. Pin
Ronald Schlenker31-Oct-13 2:46
Ronald Schlenker31-Oct-13 2:46 
QuestionHow get the dialog result ? Pin
Penc27-Aug-13 2:54
Penc27-Aug-13 2:54 
AnswerRe: How get the dialog result ? Pin
Ronald Schlenker28-Aug-13 4:32
Ronald Schlenker28-Aug-13 4:32 
GeneralRe: How get the dialog result ? Pin
Penc1-Sep-13 23:09
Penc1-Sep-13 23:09 
GeneralRe: How get the dialog result ? Pin
J. Chris Long2-Sep-13 12:42
J. Chris Long2-Sep-13 12:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.