DialogResult problem in wpf
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 th开发者_如何转开发e 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"...
Setting DialogResult
closes the window, so you can't set DialogResult
again
BTW, new Window().ShowDialog()
returns a bool?
, not a window...
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.
精彩评论