Click here to Skip to main content
11,411,241 members (64,069 online)
Click here to Skip to main content

Tagged as

WPF Dialog / MessageBox Manager

, 17 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Creating and layering message, progress and wait dialogs in WPF


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  

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:

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

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.

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:

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++)
            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.  


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.


	.CreateMessageDialog("This is the message")
	.Caption("My Title")
	.Ok(() =>
			// do something here...


  • v1 on 2013-03-17


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


About the Author

Ronald Schlenker
Software Developer (Senior)
Germany Germany
No Biography provided

Comments and Discussions

QuestionGreat Control - One Issue PinmemberNemanja Cvetkovic27-Feb-15 4:02 
Questionvery usefull PinmemberJasRaj Bishnoi12-Feb-15 2:17 
GeneralMy vote of 3 Pinmembernil_bond_0077-Nov-14 1:07 
QuestionResize Windows while dialog is "open" PinmemberMember 1038187619-Jun-14 5:00 
QuestionWPF Dialog / MessageBox Manager in a Non ViewModels system PinmemberDaniel Brito2-Apr-14 9:25 
AnswerRe: WPF Dialog / MessageBox Manager in a Non ViewModels system PinmemberMember 1082372221-May-14 12:36 
QuestionGetting DialogResult not working PinmemberCarcaB0t30-Oct-13 1:47 
AnswerRe: Getting DialogResult not working PinmemberRonald Schlenker31-Oct-13 3:56 
GeneralRe: Getting DialogResult not working PinmemberCarcaB0t31-Oct-13 10:14 
Questionclose dialog wiht buttons from the custom content. PinmemberMember 433290120-Oct-13 19:19 
AnswerRe: close dialog wiht buttons from the custom content. PinmemberRonald Schlenker31-Oct-13 3:46 
QuestionHow get the dialog result ? PinmemberPenc27-Aug-13 3:54 
AnswerRe: How get the dialog result ? PinmemberRonald Schlenker28-Aug-13 5:32 
GeneralRe: How get the dialog result ? PinmemberPenc2-Sep-13 0:09 
GeneralRe: How get the dialog result ? PinmemberJ. Chris Long2-Sep-13 13:42 
Question5+ Pinmemberanthonychege17-Jul-13 0:20 
General5 PinmemberFernando E. Braz19-Mar-13 19:04 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 18 Mar 2013
Article Copyright 2013 by Ronald Schlenker
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid