Recently I came across the need to customize the look of the standard message box in the application. To do this I’ve decided to create a new class named WPFMessageBox, which will have the same interface as the standard one, will behave the same way and finally would be fully customizable, the WPF way, using control templates.
Following is the result of this effort.
Note: In the image you can see an example for a message box customization. Of course you are limited only by your imagination. The default control template looks like the standard message box.
- The class WPFMessageBox has the exact same interface as the current WPF MessageBox class.
- Implemented as a custom control, thus fully customizable via standard WPF control templates.
- Has a default control template which looks like the standard MessageBox.
- Supports all the common types of message boxes: Error, Warning, Question and Information.
- Has the same “Beep” sounds as when opening a standard MessageBox.
- Supports the same behavior when pressing the Escape button as the standard MessageBox.
- Provides the same system menu as the standard MessageBox, including disabling the Close button when the message box is in Yes-No mode.
- Handles right-aligned and right-to-left operating systems, same as the standard MessageBox.
- Provides support for setting the owner window as a WinForms Form control.
How to use it?
Using the WPFMessageBox class is (exactly) as simple as presenting a standard MessageBox.
Instead of writing:
All of the methods of the original MessageBox are supported:
How to customize it?
The main point of creating this WPF message box was to customize it, right? so I’d better show you how this is done.
To customize the WPFMessageBox we use the standard control templates way:
where the msgbox prefix is defined as follows:
Of course, you can use Blend to do the customizations.
Can I see a real example of such customization?
Sure, following is the control template for the message box you see in the image. Note the binding you need to use to properly set the visibility of the buttons and the image.
Where can I get it?
I’ve uploaded the source to a new CodePlex project named WPF MessageBox.
That’s it for now,