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


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;		
}
 
 



 Comments on this article

Show all comments
Suhas Kulkarni
Commented on 27.October 2009
its so easy :)
cool
Commented on 19.November 2009
This is cool, beautiful
unni
Commented on 2.December 2009
getting an exception "DialogResult can be set only after Window is created and shown as dialog" while clicking OK button :(
Anand
Commented on 10.December 2009
getting an exception "DialogResult can be set only after Window is created and shown as dialog" while clicking OK button :(
TV
Commented on 14.December 2009
<Button Content="Cancel" IsCancel="True" />
Doen't seem to work .. it's not closing the window.
pooja
Commented on 4.January 2010
getting an exception "DialogResult can be set only after Window is created and shown as dialog" while clicking OK button :(
su
Commented on 5.January 2010
I guess, the missing bit of code is the one that creates a dialog box!
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();
}

Name
E-Mail (optional)
Comment
About Christian Moser