Click here to Skip to main content
13,354,605 members (58,535 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 11 Jul 2013

All-Purpose Message Box in WPF

, 3 Mar 2014
Rate this:
Please Sign up or sign in to vote.
Avoiding a cumbersome message box or input box that locks the application in WPF.


Showing a Message Box, an Input Box or a Quick Popup Form may be achieved in great many different ways.
However, it is rather annoying to have to open or manage another window or special dialog just for that, after all, a message box functionality should really be quite simple, shouldn't it?

In addition, when you want further complexity in that message box or window and when you want it to communicate with the main window, it starts to get a little messy...


So, what's new in this Tip?

As we stated above there are many ways to achieve the functionality of a Message Box,

Some examples would be:

  • Use a dialog, there's a great example of that in this CP article: WPF-Dialog-MessageBox-Manager
  • Use a simple MessageBox [System.Windows.MessageBox] as in the code sample below:
MessageBoxResult result = 
  MessageBox.Show("Do you want to close this window?", 
  "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)

So, what's the problem?

Well, these approaches as well as several others will lock the application, and are quite cumbersome to style and manage. What we have in this Tip is a lightweight, flexible approach, that avoids the quite horrible locking behaviour of using external Win32-like approaches.

Using the code

So how is it accomplished?


The basic idea is to add a Grid at the end of your XAML, initially with Visibility set to Collapsed. And then simply toggle the Visibility property to Visible when the MessageBox (or InputBox\Form etc) should appear. Upon confirmation (or cancellation), perform the needed tasks and reset the Visibility to Collapsed.

The Grid will have a semi transparent black background, and will contain the Form\MessageBox\InputBox etc in the middle, as any control.

The code and XAML are extremely simple, as you can see below.


<Window x:Class="DialogReplacement.dialogExample"



        Title="dialogExample" Height="500" Width="500">
    <Grid x:Name="gridMain">
        <Button Margin="20" Width="200" Background="Black" 

            Foreground="White" Content="Click to toggle a Confirm Dialog" 

            Click="OpenMbox_Clicked" />
            Your Page content here
        <Grid x:Name="DialogReplacement" Visibility="Collapsed">
            <Grid Background="Black" Opacity="0.5"/>







                    <Button x:Name="btnClose" 



                            Height="20" Width="20" 

                            Content="X" FontSize="12" 

                            FontFamily="Georgia" FontWeight="Bold" 

                            Foreground="WhiteSmoke" Background="Red" 

                            Click="mbox_cancel" />
                    <StackPanel HorizontalAlignment="Center" Margin="0,-22,0,0">
                        <Label FontFamily="Cambria" Content="Confirm Dialog" 

                               FontWeight="Bold" FontSize="20" />
                        <Label FontSize="14" FontWeight="Bold" Foreground="White" 

                               Content="Are you sure?"></Label>
                    <Button HorizontalAlignment="Right" x:Name="YesButton" 

                        Width="40" Margin="8" 


                        Content="Yes" Background="Olive" 



Code Behind:

namespace DialogReplacement
    public partial class dialogExample : Window
        public dialogExample()
        private void OpenMbox_Clicked(object sender, RoutedEventArgs e)
            DialogReplacement.Visibility = System.Windows.Visibility.Visible;
        private void mbox_ok(object sender, RoutedEventArgs e)
            DialogReplacement.Visibility = System.Windows.Visibility.Collapsed;
        private void mbox_cancel(object sender, RoutedEventArgs e)
            DialogReplacement.Visibility = System.Windows.Visibility.Collapsed;


Here's an example in which I almost deleted Chris' account...

Oh don't worry, I've clicked cancel ;)


  • Version


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


About the Author

Joezer BH
Chief Technology Officer
United States United States
Senior dish washing consultant for over 25 years!

You may also be interested in...

Comments and Discussions

QuestionGood job Pin
1.21 Gigawatts4-Aug-14 3:39
member1.21 Gigawatts4-Aug-14 3:39 
AnswerRe: Good job Pin
Canny Brisk6-Aug-14 5:17
professionalCanny Brisk6-Aug-14 5:17 
QuestionLooks nice Pin
johannesnestler3-Mar-14 2:00
memberjohannesnestler3-Mar-14 2:00 
AnswerRe: Looks nice Pin
Canny Brisk3-Mar-14 2:30
professionalCanny Brisk3-Mar-14 2:30 
QuestionThe style is really nice Pin
Herbisaurus12-Aug-13 21:12
memberHerbisaurus12-Aug-13 21:12 
Do you have any more of those Tips?
AnswerRe: The style is really nice Pin
Maimonides12-Aug-13 21:14
professionalMaimonides12-Aug-13 21:14 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 3 Mar 2014
Article Copyright 2013 by Joezer BH
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid