Home  separator  Controls  separator  Dialogs
Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  


Dialogs in WPF

OK and Cancel Buttons in a Dialog

You have a modal dialog with several buttons on it and you want to automatically close it, when the user presses on some of them. To do this you have to set IsCancel="true" on all buttons that should close the dialog and return false. On one button you set IsDefault="true" this will be executed when you press [Enter]. It closes the dialog and returns... also false. To return true here you have to register a callback that sets the DialogResult to true

 
<Window	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Button Content="Cancel" IsCancel="True" />
        <Button Click="OkClick" Content="Ok" IsDefault="true" />
    </StackPanel>
</Window>
 
 
 
private void OkClick(object sender, RoutedEventArgs e)		
{
    this.DialogResult = true;		
}
 
 




Last modified: 2009-10-09 16:41:46
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Doaa
Commented on 4.February 2010
I got the same exception "DialogResult can be set only after Window is created and shown as dialog" while clicking OK button"
what 's wrong.
Alex Che
Commented on 5.February 2010
You need to call ShowDialog() method of this window class to show it.
nvs
Commented on 15.February 2010
While calling DiaLogWindow.ShowDialog()from App.xaml.cs in OnStartup method, when I set this.DialogResult = true in OkButton_Click of DiaLogWindow, then complete application is going to close. So Please let me know the isues here...
Yisangchol
Commented on 18.March 2010
if you wanna execute this sample . firstly make other wpf window. secondly make new window object(involved this code) and use show() or showdialog() method. thx

Maggie
Commented on 12.May 2010
You should make other wpf window which include the sample code, and then invoke this dialog in MianWindow.xaml.Just make sure that you've shown the dialog using ShowDialog() rather than Show() method.
rahul bhojane
Commented on 3.June 2010
I working in wpf with vb.net , i used this code but a gives exception
so plz how to used
suma
Commented on 21.June 2010
getting an exception "DialogResult can be set only after Window is created and shown as dialog" while clicking OK button :( ..What to Do
chet
Commented on 1.July 2010
DialogResult:
Gets or sets the dialog result value, which is the value that is returned from the System.Windows.Window.ShowDialog() method.
So It only can use in the model window
gopichandan
Commented on 6.July 2010
getting an exception "DialogResult can be set only after Window is created and shown as dialog" while clicking OK button :( ..
RP
Commented on 8.July 2010
getting an exception "DialogResult can be set only after Window is created and shown as dialog" while clicking OK button any help
Ramakanta
Commented on 19.July 2010
getting an exception "DialogResult can be set only after Window is created and shown as dialog" while clicking OK button :( ..
aadsd
Commented on 27.July 2010
getting an exception "DialogResult can be set only after Window is created and shown as dialog" while clicking OK button :( ..
bindu
Commented on 10.August 2010
hey concept is really cool but the code is not working!!! getting an exception for OK and Cancel is not working...guys u r true...pls suggest a way to crack this errors
yossharel
Commented on 12.August 2010
In order to this sample will work,
you have to remove the StartupUri="Window1.xaml" from App.xaml and to App.xaml.cs this:
Window1 w = new Window();
w.ShowDialog();
yossharel
Commented on 12.August 2010
In order to this sample will work,
you have to remove the StartupUri="Window1.xaml" from App.xaml and in App.xaml.cs add this:

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

Window1 w1 = new Window1();
w1.ShowDialog();
}
felinis
Commented on 24.October 2010
This dialog tutorial is incomplete. It assums that you have already coded and created a WPF dialog box. This is not a simple task. Perform a web search on "WPF dialog" and you will be find plenty of sample WPF dialogs.

I hope that the remainder of this WPF tutorial is not just a work in progress. So far, things are not looking good...
ankita
Commented on 11.November 2010
Can anybody tell me how to use FolderBrowserDialog in WPF without using System.Windows.Forms.FolderBrowserDialog??

Thanks.
Minaj
Commented on 20.November 2010
It Is Really Fantastic! Which Gives More Idea About Designing
karthik
Commented on 22.December 2010
getting an exception "DialogResult can be set only after Window is created and shown as dialog" while clicking OK button :(
WPF Beginner
Commented on 29.December 2010
This site is a waste of time ....
turd burglar
Commented on 16.January 2011
this is shite, either do it properly or don't bother
smera
Commented on 19.January 2011
add another page and create the function dialogueresult(). call this function using the object of that where function defined.
Damitha
Commented on 27.January 2011
Getting Errer -
DialogResult can be set only after Window is created and shown as dialog.
Anurag
Commented on 25.April 2011
What the code is doing cannot understand
raju
Commented on 12.May 2011
How can we handle ESC key. i.e if u press ESC dialog is getting close. how can we stop this.

Name
E-Mail (optional)
Comment