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: 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"



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

        WindowStyle="None" ResizeMode="NoResize"


        AllowsTransparency="True" Background="Transparent">

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

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


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

                FontSize="50" TextWrapping="Wrap" 

                HorizontalAlignment="Center" VerticalAlignment="Center" 

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

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

                    Margin="1" HorizontalAlignment="Left" 

                    VerticalAlignment="Top" Width="55" 

                    <Button Content="No" x:Name="No" 

                    Margin="1" HorizontalAlignment="Left" 

                    VerticalAlignment="Top" Width="55" 

                    Click="No_Click" IsCancel="True"/>


public partial class MsgBoxYesNo : Window
    public MsgBoxYesNo(string message)

        txtMessage.Text = message;

    private void Yes_Click(object sender, RoutedEventArgs e)
        DialogResult = true;

    private void No_Click(object sender, RoutedEventArgs e)
        DialogResult = false;


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!");
        MessageBox.Show("You're kidding, of course you're awesome!");


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


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


3 bookmarked