Click here to Skip to main content
14,542,129 members

Custom YES/NO Dialog with DialogResult in WPF

Rate this:
4.00 (2 votes)
Please Sign up or sign in to vote.
4.00 (2 votes)
23 Sep 2014CPOL
This is a simple example of how to make a custom YES/NO dialog with DialogResult in WPF.

Here is a simple example of how to make a custom YES/NO dialog with DialogResult in WPF. Freely (as in beer) available code is on Github: https://github.com/Hitman666/CustomYesNoDialogWPF. The breakdown is that you make a new window (MsgBoxYesNo) and assign DialogResult on the button clicks. You call this window as a dialog from a MainWindow and expect a bool in return upon which you act as you wish… MsgBoxYesNo.xaml:

<Window x:Class="CustomYesNoDialogWPF.MsgBoxYesNo"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MsgBoxYesNo" Height="300" Width="300"

        WindowStyle="None" ResizeMode="NoResize"

        WindowStartupLocation="CenterScreen"

        AllowsTransparency="True" Background="Transparent">

    <Border BorderThickness="5" BorderBrush="Black" 

    CornerRadius="20" Background="SkyBlue">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="3*"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <Viewbox>
                <TextBlock x:Name="txtMessage" Width="420" 

                FontSize="50" TextWrapping="Wrap" 

                HorizontalAlignment="Center" VerticalAlignment="Center" 

                TextAlignment="Center" Text="Are you sure you're awesome?"/>
            </Viewbox>

            <Viewbox Grid.Row="1">
                <StackPanel Orientation="Horizontal">
                    <Button Content="Yes" x:Name="Yes" 

                    Margin="1" HorizontalAlignment="Left" 

                    VerticalAlignment="Top" Width="55" 

                    Click="Yes_Click"/>
                    <Button Content="No" x:Name="No" 

                    Margin="1" HorizontalAlignment="Left" 

                    VerticalAlignment="Top" Width="55" 

                    Click="No_Click" IsCancel="True"/>
                </StackPanel>
            </Viewbox>
        </Grid>
    </Border>
</Window>

MsgBoxYesNo.cs:

public partial class MsgBoxYesNo : Window
{
    public MsgBoxYesNo(string message)
    {
        InitializeComponent();

        txtMessage.Text = message;
    }

    private void Yes_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = true;
        this.Close();
    }

    private void No_Click(object sender, RoutedEventArgs e)
    {
        DialogResult = false;
        this.Close();
    }
}

MainWindow.cs:

private void Button_Click(object sender, RoutedEventArgs e)
{
    MsgBoxYesNo msgbox = new MsgBoxYesNo("Are you sure you're awesome?");
    if ((bool)msgbox.ShowDialog())
    {
        MessageBox.Show("Yes, you're awesome!");
    }
    else
    {
        MessageBox.Show("You're kidding, of course you're awesome!");
    }
}

License

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

Share

About the Author

Nikola Breznjak
Software Developer (Senior)
Croatia Croatia
I’m an engineer at heart and a jack of all trades kind of guy.

For those who care about titles, I hold a masters degree in computing from FER (and a black belt in karate, but that’s another story…).

During the last years, worked in a betting software industry where I made use of my knowledge in areas ranging from full-stack (web & desktop) development to game development through Linux and database administration and use of various languages (C#, PHP, JavaScript to name just a few).

Currently, I’m a senior software engineer at TelTech, where we make innovative communications apps, and I <3 it.

Lately, I’m very passionate about Ionic framework and am currently in the top 3 answerers on StackOverflow in Ionic framework. I wrote a book about Ionic framework which you can get for free on Leanpub: Ionic framework – step by step from idea through prototyping to the app stores.

Other technical writing:

+ wrote a book Getting MEAN with MEMEs
was a technical reviewer for a book Deploying Node published by Packt
was a technical reviewer for a book Getting started with Ionic published by Packt
After writing 300 posts, this is why I think you should start blogging too

Come and see what I write about on my blog.

Comments and Discussions

 
-- There are no messages in this forum --
Technical Blog
Posted 23 Sep 2014

Tagged as

Stats

20K views
3 bookmarked