[英]WPF Window ShowDialog() causing Cannot set Visibility or call Show
I am creating a wpf form which is going to be used for adding/editing data from datagrid. 我正在创建一个wpf表单,该表单将用于从datagrid添加/编辑数据。 However when I check for
ShowDialog() == true
I am getting the above exception. 但是,当我检查
ShowDialog() == true
时,出现上述异常。
The code is taken from a book (Windows Presentation Foundation 4.5 Cookbook). 该代码摘自一本书(Windows Presentation Foundation 4.5 Cookbook)。
UserWindow usrw = new UserWindow();
usrw.ShowDialog();
if (usrw.ShowDialog() == true)
{
//do some stuff here;
}
And on the WPF window: 在WPF窗口上:
private void btn_Save_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
How I can handle this? 我该如何处理?
=============================== ===============================
The solution to the problem was simply to remove usrw.ShowDialog(); 解决该问题的方法只是删除usrw.ShowDialog();。 and it start working as expected
它开始按预期工作
UserWindow usrw = new UserWindow();
//usrw.ShowDialog();
if (usrw.ShowDialog() == true)
{
//do some stuff here;
}
You are trying to open your window 2 times with every call to ShowDialog()
您尝试每次调用
ShowDialog()
两次打开窗口
try 尝试
UserWindow usrw = new UserWindow();
bool result =(bool)usrw.ShowDialog();
if (result)
{
//do some stuff here;
}
or 要么
UserWindow usrw = new UserWindow();
usrw.ShowDialog();
if ((bool)usrw.DialogResult)
{
//do some stuff here;
}
keep in mind that DialogResult
is Nullable. 请记住,
DialogResult
是可为空的。 If there is a chance that you are closing the window without setting the DialogResult, check for null
. 如果您有可能在没有设置DialogResult的情况下关闭窗口,请检查
null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.