MessageBox

Derives from Xceed.Wpf.Toolkit.Primitives.WindowControl

Starting with version 2.0, MessageBox (and ChildWindow) is derived from WindowControl and no longer manages its parent’s background or the positioning of itself based on its parent’s size.

A WindowContainer should now be used to contain these controls. It provides an area where WindowControl-derived controls (ChildWindow or MessageBox controls) can be displayed. This is particularly interesting in an XBAP application where windows can't be popped up. In this case the WindowContainer can be sized to fit the application and the window-like control can be moved around in the WindowContainer.

The WindowContainer derives from Canvas and positions its children according to its size. Many actions performed on its children are managed by the WindowContainer (movement and positioning, resizing, visibility, modal, and mouse click). This will restrict the WindowControl movements and resizing to the WindowContainer’s size.

When no Width and Height are specified in the WindowContainer, its DesiredSize will be the size of its biggest child.


Buttons
messagebox_buttons.jpg

Icons
messagebox_icons.jpg

Usage

The MessageBox mimics the behavior of the System.Windows.MessageBox closely. You use similar syntax to create and show a message box.

      MessageBoxResult result =  Microsoft.Windows.Controls.MessageBox.Show("Hello world!", "Extended WPF ToolKit MessageBox", MessageBoxButton.OK, MessageBoxImage.Question);
      MessageBoxResult result =  Microsoft.Windows.Controls.MessageBox.Show("Hello world!", "Extended WPF ToolKit MessageBox", MessageBoxButton.OK);
      MessageBoxResult result =  Microsoft.Windows.Controls.MessageBox.Show("Hello world!", "Extended WPF ToolKit MessageBox");
      MessageBoxResult result =  Microsoft.Windows.Controls.MessageBox.Show("Hello world!");


Style Guide

You can style the MessageBox to fit the theme of your application by using a few of the MessageBox's properties. The following image depicts what properties modify which parts of the MessageBox control.
messagebox_style_guide.jpg

Properties

Property Description
ButtonRegionBackground Gets or sets the background of the region behind the message box buttons.
CancelButtonContent Gets or sets the content of the Cancel button.
CancelButtonStyle Gets or sets the style of the Cancel button.
Caption Gets or sets the caption of the message box. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
CaptionForeground Gets or sets the font color of the caption. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
CaptionIcon Gets or sets the caption icon. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
CaptionShadowBrush Gets or sets the brush of the caption's shadow. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
CloseButtonStyle Gets or sets the style of the close button. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
CloseButtonVisibility Gets or sets the visibility of the Close button. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
ImageSource Gets or sets the ImageSource.
IsActive Gets or sets whether the control is active. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
Left Gets or sets the left side of the control. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
MessageBoxResult Gets the MessageBox result, which is set when the Closed event is raised.
NoButtonContent Gets or sets the content of the No button.
NoButtonStyle Gets or sets the style of the No button.
OkButtonContent Gets or sets the content of the OK button.
OkButtonStyle Gets or sets the OK button's style.
Text Gets or sets the text of the message box.
Top Gets or sets the top side of the control. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
WindowBackground Gets or sets the background color of the MessageBox window. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
WindowBorderBrush Gets or sets the color of the outer border of the MessageBox. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
WindowBorderThickness Gets or sets the window's border thickness. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
WindowInactiveBackground Gets or sets the window's inactive background. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
WindowOpacity Gets or sets the opacity of the MessageBox, but does not effect the close button or the message text area. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
WindowStyle Gets or sets the window's style. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
WindowThickness Gets or sets the window's thickness. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
YesButtonContent Gets or sets the content of the Yes button.
YesButtonStyle Gets or sets the Yes button's style.

Events

Event Description
CloseButtonClicked Raised when the Close button is clicked. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
Closed Raised when the message box is closed.
HeaderDragDelta Raised as the mouse changes position when a header has logical focus and mouse capture. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
HeaderIconClicked Raised when the header icon is clicked. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
HeaderIconDoubleClicked Raised when the header icon is double-clicked. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
HeaderMouseLeftButtonClicked Raised when the header is clicked using the left mouse button. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
HeaderMouseLeftButtonDoubleClicked Raised when the header is double-clicked using the left mouse button. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)
HeaderMouseRightButtonClicked Raised when the header is clicked using the right mouse button. (Inherited from Xceed.Wpf.Toolkit.Primitives.WindowControl)

Methods

Method Description
Show Overloaded. Displays a message box.
ShowDialog Display the MessageBox window and returns only when this MessageBox closes.
ShowMessageBox Overloaded. Displays this message box when embedded in a WindowContainer parent.

Example

You can style all instances of your MessageBox control by placing the style definition markup inside your App.xaml file under the resources section. To create the following ugly style requires the following markup.

style_example.jpg

        <SolidColorBrush x:Key="MyButtonHoverBrush" Color="YellowGreen" />
        <SolidColorBrush x:Key="MyButtonPressedBrush" Color="Maroon" />
        
        <Style x:Key="MyCloseButtonStyle" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background)">
                                                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{StaticResource MyButtonHoverBrush}"></DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background)">
                                                <DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{StaticResource MyButtonPressedBrush}"></DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="Background" CornerRadius="0,0,2,0" Background="Green">
                                <Border Margin="1,0,1,1" BorderBrush="#59FFFFFF" BorderThickness="1" CornerRadius="0,0,1,0"/>
                            </Border>
                            <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                            <Path x:Name="path" Fill="White" Margin="0,0,0,1" Visibility="Collapsed"
	                Height="6"
		Width="7"
		Stretch="Fill"
		Opacity="1"
		Data="M 2,6 C2,6 3,6 3,6 3,6 3,5 3,5 3,5 4,5 4,5 4,5 4,6 4,6 4,6 5,6 5,6 5,6 7,6 7,6 7,6 7,5 7,5 7,5 6,5 6,5 6,5 6,4 6,4 6,4 5,4 5,4 5,4 5,2 5,2 5,2 6,2 6,2 6,2 6,1 6,1 6,1 7,1 7,1 7,1 7,0 7,0 7,0 5,0 5,0 5,0 4,0 4,0 4,0 4,1 4,1 4,1 3,1 3,1 3,1 3,0 3,0 3,0 2,0 2,0 2,0 0,0 0,0 0,0 0,1 0,1 0,1 1,1 1,1 1,1 1,2 1,2 1,2 2,2 2,2 2,2 2,4 2,4 2,4 1,4 1,4 1,4 1,5 1,5 1,5 0,5 0,5 0,5 0,6 0,6 0,6 2,6 2,6 z"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        
        <Style TargetType="{x:Type toolkit:MessageBox}">
            <Setter Property="Background" Value="Aquamarine" />
            <Setter Property="BorderBrush" Value="Red" />
            <Setter Property="CaptionForeground" Value="Orange" />
            <Setter Property="WindowBorderBrush" Value="Blue" />
            <Setter Property="WindowBackground" Value="Green" />
            <Setter Property="WindowOpacity" Value="0.3" />
            <Setter Property="Foreground" Value="Purple"/>
            <Setter Property="CloseButtonStyle" Value="{StaticResource MyCloseButtonStyle}"/>
        </Style>



Support this project, check out the Plus Edition.
---

Last edited Jun 10 at 3:48 PM by dlafontaine, version 40

Comments

devgig Jul 11 at 3:46 PM 
When a Style is applied mnemonics are enabled not enabled for Yes/No/Cancel/Ok/Etc... Do you know what we can do in XAML to fix this?

Konstantinos May 24 at 8:36 PM 
OK people, this is how you change the image (at least in my version of the library):

var res = Xceed.Wpf.Toolkit.MessageBox.Show(
"MsgConfirmDeleteSelectedRows",
"MsgTltConfirm",
MessageBoxButton.YesNo,
MessageBoxImage.None, MessageBoxResult.No, this.FindResource("MsgBoxExclamationStyle") as Style);


<Style x:Key="MsgBoxExclamation" TargetType="{x:Type xctk:MessageBox}" BasedOn="{StaticResource MsgBoxStyle}">
<Setter Property="ImageSource">
<Setter.Value>
<!-- Set value here with a type of : DrawingImage, BitmapSource, etc -->
</Setter.Value>
</Setter>
</Style>


"MsgBoxStyle" is the key of your normal style for the MessageBox. If you don't have any, remove the "BasedOn" attribute.

Finding everything for yourself succckkkks... :(
I know that you will love me for this.
If you want to thank me, thank my God and become an Orthodox Christian! :D

BoucherS Aug 28, 2013 at 12:28 PM 
You can use the "ImageSource" Property from MessageBox to change the image in the MessageBox.
You can use the "CaptionIcon" Property from MessageBox to change the image of the icon at the top of the MessageBox.
Please open a dicsussion tab to discuss about this. Thanks.

tiduszhang Aug 28, 2013 at 9:27 AM 
How to change Icons By Custom Image?

BoucherS Aug 26, 2013 at 12:32 PM 
When The MessageBox is in a WindowContainer, use this :
_messageBox.ShowMessageBox( "data", "title", MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.OK );

tiduszhang Aug 26, 2013 at 8:22 AM 
How to change Icons ?

mikem4a1 Jun 6, 2013 at 7:36 PM 
Usage needs to be updated. Here is a proper example: Xceed.Wpf.Toolkit.MessageBox.Show("Unable to connect.", "Server Connection Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);

Dev_Matthias Feb 15, 2013 at 12:28 PM 
Thank you very much!
When is the 2.0 planned to release? i hope ASAP :)

BoucherS Feb 15, 2013 at 11:59 AM 
MessageBoxes will work in XBAP in the version 2.0 of the toolkit.

Dev_Matthias Feb 15, 2013 at 10:13 AM 
I have a problem with MessageBox while using it a XBAP application. i get an error when the messagebox must show a dialog:
System.InvalidOperationException: Cannot perform this operation when hosted in browser.
at MS.Internal.AppModel.RootBrowserWindow.VerifyApiSupported()
It would be great if its supported in XBAP!! :)

Konstantinos Nov 17, 2012 at 11:31 PM 
"Is there a way to use this "non-client area replacement" (I think it's called a chrome) in a custom Window?"

I just saw you have the class ChildWindow.
I guess nothing yet for windows with minimize/maximize buttons, etc, ah?

Konstantinos Nov 17, 2012 at 11:14 PM 
Nice tools you've built here!

Is there a way to use this "non-client area replacement" (I think it's called a chrome) in a custom Window?
I didn't see any documentation on that.

And you could also make the MyCloseButtonStyle above set Focusable to False.

Also, in your DateTimePicker you could draw the drop-down button arrow using a path. It seems digitized when you use a scale transform on it. Also I haven't found a way to change the content of the button that says 'Advanced' on the DateTimePicker (in .NET code at least - haven't tried with ControlTemplates). I use version 1.6 (I think). Perhaps you've changed some things since then.

xqandpan Nov 7, 2012 at 1:23 AM 
I want to customize the MessageBoxImage, how can I do it?

BoucherS Oct 11, 2012 at 8:14 PM 
In v1.8, there are new Properties : OkButtonStyle, CancelButtonStyle, YesButtonStyle and NoButtonStyle that let you specify the style of each button.

zhaolq85 Jun 18, 2012 at 3:07 AM 
I want to modify the style of OK and Cancle button .But I don't how?