4

I have a window that I open a few times as a ShowDialog.

Every time I open it I use the new keyword in the first time i did:

var myWindow = new MyWindow();
myWindow.ShowDialog();

second time I open it from the MyWindow View Model class:

new MyWindow().ShowDialog();

in MyWindow code behind I have 2 events. one is when the user clcik ok and another when the user click cancel.

void OnCancel(){
  DialogResult = false;
}

void OnOk(){
  DialogResult = true;
}

The events fires from the View Model class form the ICommand Execute than bind to "ok" and "cancel" buttons of the window.

In the xaml I did this for the cancel button:

IsCancel = true;

And this for the ok button:

IsDefault = true;

in the first time that I opened the window I can set DialogResult = true, but after that when I try to set the DialogResult I've got exception "Dialofresult can set only after created window and shown as ShwDialog".

I also saw that the DialogResult is true after the first time is set to true and I think that the reason for the exception but I dont understand why is stay true if I closed the window and create a new one by using the new keyword...

Any suggestion

Thanks in advance

Edit: The problem is that once I clcik the "ok" button the DialogResult set to true and saty true and I can't set it to false.

Edit

Thanks everyone I solve the problem.

The problem was that I register to the View Model events ("ok" clicked and "cancel" clicked) and I remove the register when the user click "cancel" but not when he click "ok"...

Maya
  • 989
  • 4
  • 12
  • 19

2 Answers2

2

Setting DialogResult closes the window, so you can't set DialogResult again

BTW, new Window().ShowDialog() returns a bool?, not a window...

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Can u explain? I missed somthing, why I cant set it again if I create a new window and display it as ShowDialog?. And I know its return bool?. – Maya Jul 18 '11 at 14:06
  • OK... could you show more code? From the code you posted, it's not very clear what the problem is – Thomas Levesque Jul 18 '11 at 14:12
  • And you're assigning the result to a variable named `myWindow`, that's why I assumed you thought it returned a window... – Thomas Levesque Jul 18 '11 at 14:12
  • @Thomas: the interesting part of `Window.DialogResult` is it doesn't make it obvious that setting it closes the window in the documentation. Also @Maya: if you set a button as `IsCancel` it will automatically set `DialogResult` to false, which may be why you're receiving the exception. – user7116 Jul 18 '11 at 14:32
  • @sixlettervariables, as a matter of fact, `DialogResult` is *not* set automatically to false; it was the case in WinForms, but in WPF you have to set it manually. @Maya, sorry, I really don't know what is causing the problem... – Thomas Levesque Jul 18 '11 at 14:35
  • @Thomas: [MSDN](http://msdn.microsoft.com/en-us/library/system.windows.window.dialogresult.aspx) "By default, a dialog box is canceled when a user does one of the following: Presses *ALT+F4*; Clicks the Close button; Selects Close from the System menu. In all of these cases, `DialogResult` is false by default. A dialog box typically provides a special button to cancel a dialog, which is the button whose `IsCancel` property is set to true...will automatically close a window when either it is pressed, or when the ESC key is pressed. In either of these cases, `DialogResult` remains false." – user7116 Jul 18 '11 at 14:38
  • @sixlettervariables IsCancel = true in my "cancel" button but the exception throw after I clicked "ok" – Maya Jul 18 '11 at 14:41
  • @Maya: if you put a breakpoint on `DialogResult = true;` how many times is it hit? – user7116 Jul 18 '11 at 14:42
  • @sixlettervariables every time I clcik "ok". in the first time the DialogResult is null but after that is true. with the "cancel" button i think everything work fine. – Maya Jul 18 '11 at 14:45
  • @sixlettervariables now I saw that if I click the ok button and that clcik the cancel I get the exception becuase the DialogResult already set to true – Maya Jul 18 '11 at 14:51
  • @sixlettervariables, my mistake, I misremembered – Thomas Levesque Jul 18 '11 at 15:03
1

I dont understand why is stay true if I closed the window and create a new one by using the new keyword...

Because your intializing a new Window. It returns True because as you already explained the first time it does.

Security Hound
  • 2,577
  • 3
  • 25
  • 42
  • but the previous window is disposed so why when I created a new one is DialogResult is true and not null? – Maya Jul 18 '11 at 15:08