A Customizable WPF MessageBox
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.
Features
- 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:
MessageBoxResult result = MessageBox.Show
("message text", "caption", MessageBoxButton.YesNoCancel, MessageBoxImage.Error);
simply write:
MessageBoxResult result = WPFMessageBox.Show
("message text", "caption", MessageBoxButton.YesNoCancel, MessageBoxImage.Error);
All of the methods of the original MessageBox
are supported:
public static class WPFMessageBox
{
public static MessageBoxResult Show(string messageBoxText);
public static MessageBoxResult Show(string messageBoxText, string caption);
public static MessageBoxResult Show(Window owner, string messageBoxText);
public static MessageBoxResult Show
(string messageBoxText, string caption, MessageBoxButton button);
public static MessageBoxResult Show
(Window owner, string messageBoxText, string caption);
public static MessageBoxResult Show(string messageBoxText,
string caption, MessageBoxButton button, MessageBoxImage icon);
public static MessageBoxResult Show(Window owner,
string messageBoxText, string caption, MessageBoxButton button);
public static MessageBoxResult Show(string messageBoxText,
string caption, MessageBoxButton button, MessageBoxImage icon,
MessageBoxResult defaultResult);
public static MessageBoxResult Show(Window owner,
string messageBoxText, string caption, MessageBoxButton button,
MessageBoxImage icon);
public static MessageBoxResult Show(string messageBoxText,
string caption, MessageBoxButton button, MessageBoxImage icon,
MessageBoxResult defaultResult, MessageBoxOptions options);
public static MessageBoxResult Show(Window owner,
string messageBoxText, string caption, MessageBoxButton button,
MessageBoxImage icon, MessageBoxResult defaultResult);
public static MessageBoxResult Show(Window owner,
string messageBoxText, string caption, MessageBoxButton button,
MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options);
}
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:
<Style TargetType="msgbox:WPFMessageBoxControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type msgbox:WPFMessageBoxControl}">
<!-- your new template here -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
where the msgbox prefix is defined as follows:
xmlns:msgbox="clr-namespace:MessageBoxUtils;assembly=WPFMessageBox"
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.
<Style TargetType="{x:Type msgbox:WPFMessageBoxControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type msgbox:WPFMessageBoxControl}">
<Grid Background="LightBlue"
FlowDirection="{Binding ContentFlowDirection}">
<Grid.RowDefinitions>
<RowDefinition Height="55" />
<RowDefinition Height="2" />
<RowDefinition Height="*" />
<RowDefinition Height="40" />
<RowDefinition Height="55" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Message}" Grid.RowSpan="2"
Grid.Column="0" Grid.ColumnSpan="2" TextWrapping="Wrap"
TextAlignment="Left" HorizontalAlignment=
"{Binding ContentTextAlignment}" VerticalAlignment="Top"
Margin="10 10 10 10" />
<Border Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"
Background="Yellow">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center"
HorizontalAlignment="Right" Margin="0 0 5 0" >
<Button Content="_Yes" Visibility="{Binding YesNoVisibility}"
Command="{Binding YesCommand}"
IsDefault="{Binding IsYesDefault}" Margin="5 5 5 5"
Height="24" Width="80" />
<Button Content="_No" Visibility="{Binding YesNoVisibility}"
Command="{Binding NoCommand}"
IsDefault="{Binding IsNoDefault}"
Margin="5 5 5 5" Height="24" Width="80" />
<Button Content="O_K" Visibility="{Binding OkVisibility}"
Command="{Binding OkCommand}"
IsDefault="{Binding IsOkDefault}" Margin="5 5 5 5"
Height="24" Width="80" />
<Button Content="_Cancel" Visibility=
"{Binding CancelVisibility}" Command="{Binding CancelCommand}"
IsDefault="{Binding IsCancelDefault}" Margin="5 5 5 5"
Height="24" Width="80" />
</StackPanel>
</Border>
<StackPanel Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"
Orientation="Horizontal">
<Image Source="{Binding MessageImageSource}"
HorizontalAlignment="Left" VerticalAlignment="Center"
Height="32" Width="32" Margin="10 0 0 0" />
<Image Source="{Binding MessageImageSource}"
HorizontalAlignment="Left" VerticalAlignment="Center"
Height="32" Width="32" Margin="10 0 0 0" />
<Image Source="{Binding MessageImageSource}"
HorizontalAlignment="Left" VerticalAlignment="Center"
Height="32" Width="32" Margin="10 0 0 0" />
<Image Source="{Binding MessageImageSource}"
HorizontalAlignment="Left" VerticalAlignment="Center"
Height="32" Width="32" Margin="10 0 0 0" />
<Image Source="{Binding MessageImageSource}"
HorizontalAlignment="Left" VerticalAlignment="Center"
Height="32" Width="32" Margin="10 0 0 0" />
<Image Source="{Binding MessageImageSource}"
HorizontalAlignment="Left" VerticalAlignment="Center"
Height="32" Width="32" Margin="10 0 0 0" />
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Where Can I Get It?
I’ve uploaded the source to a new CodePlex project named WPF MessageBox.
That’s it for now,
Arik Poznanski.
发表评论
kWWzr4 Really cool post, highly informative and professionally written..Good Job! car donation sites
HlgkQ2 Well I sincerely enjoyed studying it. This tip procured by you is very useful for good planning.
GiwYmB There is also one more method to increase traffic in favor of your website that is link exchange, therefore you as well try it
80Tnph Wow, superb blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is great, let alone the content!. Thanks For Your article about sex.
9QhTFT Thanks-a-mundo for the blog.Much thanks again. Will read on
OwCIY2 isabel marant sneakers pas cher isabel marant sneakers pas cher
vxLXh7 Thanks again for the post.Really looking forward to read more. Really Great.
NNRH9T I value the article post.Really looking forward to read more. Keep writing.
fnbRKw I truly like your weblog put up. Preserve publishing a lot more beneficial data, we recognize it!
JNU367 It as not that I want to copy your web site, but I really like the pattern. Could you let me know which style are you using? Or was it custom made?
NI5Zlz your RSS. I don at know why I am unable to subscribe to it. Is there anyone else having similar RSS issues? Anyone that knows the answer can you kindly respond? Thanks!!
9qlloe There is perceptibly a lot to identify about this. I consider you made some good points in features also.
bM5d87 Say, you got a nice article.Much thanks again. Keep writing.
dBZhwE may you be rich and continue to guide other people.
9YeBYp Very nice info and straight to the point. I am not sure if this is truly the best place to ask but do you folks have any ideea where to hire some professional writers? Thanks
There as definately a great deal to know about this issue. I like all of the points you have made.
MhxWXb Thanks for the article post.Much thanks again. Awesome.
mEk8KE Really enjoyed this article.Much thanks again. Really Great.
vaPoEh
RDQvGn I'аve recently started a website, the information you provide on this site has helped me tremendously. Thanks for all of your time & work.
O8ZyoM Really enjoyed this blog.Thanks Again. Awesome.
JqN6lC I loved your blog.Much thanks again. Fantastic.