How to Create a DialogBox to prompt the user for Yes/No option in WPF How to Create a DialogBox to prompt the user for Yes/No option in WPF wpf wpf

How to Create a DialogBox to prompt the user for Yes/No option in WPF


Here's an example:

string sMessageBoxText = "Do you want to continue?";string sCaption = "My Test Application";MessageBoxButton btnMessageBox = MessageBoxButton.YesNoCancel;MessageBoxImage icnMessageBox = MessageBoxImage.Warning;MessageBoxResult rsltMessageBox = MessageBox.Show(sMessageBoxText, sCaption, btnMessageBox, icnMessageBox);switch (rsltMessageBox){    case MessageBoxResult.Yes:    /* ... */    break;    case MessageBoxResult.No:    /* ... */    break;    case MessageBoxResult.Cancel:    /* ... */    break;}


Please note that while Radu's answer works, you cannot apply WPF styles to the MessageBox.

I took a different approach to this problem.

I created a class to serve as a View Model for my message window and I created a style for how I wanted my window to appear. Later in code I instantiated a new Window, set it's DataContext to an instance of my View Model, and set the Window's Style property to the style I created for the window.

I know it sounds a bit overkill, and I'm not sure how other people go about solving this same issue... but my solution is quite flexible and I'm starting to really like it.

For example, here is Dialog View Model:

Public Class MyDialogViewModel  Public Event Closed()  Public Property Message As String  Public Property Cancel As MyNamespace.RelayCommand  Public Property Close As MyNamespace.RelayCommand  Public Property WasCancelled As Boolean  Public Sub New()    WasCancelled = True    Cancel = New MyNamespace.RelayCommand(AddressOf CancelClicked)    Close = New MyNamespace.RelayCommand(AddressOf CloseClicked)  End Sub  Private Sub CancelClicked()    RaiseEvent Closed()  End Sub  Private Sub CloseClicked()    WasCancelled = False    RaiseEvent Closed()  End SubEnd Class

Here is my style for a basic "message" window:

    <Style x:Key="myMessageStyle" TargetType="{x:Type myNameSpace:CustomDialogWindow}">        <Setter Property="Template">            <Setter.Value>                <ControlTemplate>                    <ControlTemplate.Resources>                        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">                            <Setter Property="Width" Value="100"/>                            <Setter Property="Height" Value="25"/>                        </Style>                    </ControlTemplate.Resources>                    <Border >                        <DockPanel Margin="10,10,0,10">                            <TextBlock Text="{Binding Message}" Width="Auto" TextWrapping="WrapWithOverflow" DockPanel.Dock="Top"                                        Margin="10"                                       Foreground="{StaticResource MyMessageBoxForegroundColor}"/>                            <DockPanel Margin="5,0,0,0" DockPanel.Dock="Bottom">                                <Button Content="Ok" Command="{Binding Close}" ></Button>                                <Button Content="Cancel" Command="{Binding Cancel}"  HorizontalAlignment="Stretch"></Button>                            </DockPanel>                        </DockPanel>                    </Border>                </ControlTemplate>            </Setter.Value>        </Setter>    </Style>

My CustomDialogWindow is simply a window with nothing in it:

(XAML)

<Window x:Class="CustomDialogWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="CustomDialogWindow"        SizeToContent="WidthAndHeight"></Window>

And in the CustomDialogWindow I have the following code so that the window closes when the user clicks cancel or ok:

Public Class CustomDialogWindow     Private Sub CustomDialogWindow_DataContextChanged(ByVal sender As Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs) Handles Me.DataContextChanged        Dim dContext As MyDialogViewModel= TryCast(DataContext, MyDialogViewModel)        If dContext IsNot Nothing Then            AddHandler DirectCast(DataContext, MyDialogViewModel).CloseWindow, AddressOf CloseWindow        End If    End Sub    Private Sub CloseWindow()        Me.Close()    End SubEnd Class

Now when I need to use the window I just instantiate a new CustomDialogWindow, set it's DataContext to a new instance of the DialogViewModel class, and set it's style to the "myMessageStyle":

Dim cdw As New CustomDialogWindowDim dvm As New DialogViewModeldvm.Message = "Hello World!"cdw.DataContext = dvmcdw.ShowDialog()If dvm.WasCancelled = False Then   '....'End If

The reason why I like this approach is because I inherit from the MyDialogViewModel and provide more properties so that, for instance, I can display a bunch of options for the user to choose from. I just supply custom styles for each type of window I want to display (making sure to bind the appropriate properties). Like I said, it's very flexible and pretty simple to implement.

Cheers!

-Frinny