Introduction
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...
Background
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)
{
Application.Current.Shutdown();
}
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?
Easy!
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.
XAML:
<Window x:Class="DialogReplacement.dialogExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="dialogExample" Height="500" Width="500">
<Grid x:Name="gridMain">
<StackPanel>
<Button Margin="20" Width="200" Background="Black"
Foreground="White" Content="Click to toggle a Confirm Dialog"
Click="OpenMbox_Clicked" />
-->
</StackPanel>
<Grid x:Name="DialogReplacement" Visibility="Collapsed">
<Grid Background="Black" Opacity="0.5"/>
<Border
MinWidth="250"
Background="DarkGoldenrod"
BorderBrush="Black"
BorderThickness="1"
CornerRadius="0,65,0,65"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<StackPanel>
<Button x:Name="btnClose"
Margin="8"
HorizontalAlignment="Left"
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>
</StackPanel>
<Button HorizontalAlignment="Right" x:Name="YesButton"
Width="40" Margin="8"
Padding="3,0,3,0"
Content="Yes" Background="Olive"
Foreground="White"
Click="mbox_ok"/>
</StackPanel>
</Border>
</Grid>
</Grid>
</Window>
Code Behind:
namespace DialogReplacement
{
public partial class dialogExample : Window
{
public dialogExample()
{
InitializeComponent();
}
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;
}
}
}
Example
Here's an example in which I almost deleted Chris' account...
Oh don't worry, I've clicked cancel
History