DialogResult的问题在WPF [英] DialogResult problem in wpf

查看:977
本文介绍了DialogResult的问题在WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我开了几次作为ShowDialog的一个窗口。

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

每当我打开它,我使用new关键字
在第一时间我所做的:

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

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



第二次我打开它从mywindow的视图模型类:

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

new MyWindow().ShowDialog();

在mywindow的代码隐藏我有两个事件。一个是当用户clcik确定,另一个当用户点击取消。

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



从视图模型类的事件火灾形成的ICommand比绑定执行以 。确定和取消窗口的按钮

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

在XAML我这样做了取消键:

In the xaml I did this for the cancel button:

IsCancel = true;

和本作的OK按钮:

IsDefault = true;

在我第一次打开我可以设置的DialogResult =真正的窗口,但在那之后,当我尝试设置我得异常Dialofresult只能设置在创建后的窗口,并显示为ShwDialog中的DialogResult。

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

我也看到,DialogResult的是第一个真正的后时间设置为true。我认为,对异常的原因,但我不明白这是为什么,如果我关上窗户通过使用new关键字...

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...

任何建议

在此先感谢

编辑:
中的问题是,一旦我clcik确定按钮的DialogResult设置为true,SATY真实,我不能将其设置为false。

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.

修改

谢谢大家我解决这个问题。

Thanks everyone I solve the problem.

的问题是,我注册视图模型的事件(OK,并点击取消点击),并删除了寄存器,当用户点击取消,而不是当他点击确定...

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

推荐答案

设置的DialogResult 关闭窗口,所以你不能设置的DialogResult 再次

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

BTW,新窗口()。的ShowDialog()返回布尔?,而不是一个窗口...

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

这篇关于DialogResult的问题在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆