我正在开发多实例WPF应用程序。应用程序在主屏幕中有一个网格,双击网格行,它会打开子窗口。它还具有从主屏幕双击网格行打开多个子窗口的功能。
如果子窗口处于活动状态,有人可以帮助我防止父窗口关闭吗?因此,如果子窗口处于活动状态,则用户无法关闭主窗口。
发布于 2015-08-03 21:33:22
将这些childs的Owner属性设置为Main Windows:
private void Button_Click(object sender, RoutedEventArgs e)
{
var wnd = new Window();
wnd.Owner = this;
wnd.Show();
}然后在主窗口关闭事件处理程序中:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.OwnedWindows.Count > 0)
{
MessageBox.Show("Child windows exists, you have to close'em first");
e.Cancel = true;
}
}作为最后一点,它可能会帮助你知道,你可以从你的代码中的任何地方获得应用程序的主窗口,如下所示:
System.Windows.Application.Current.MainWindow因此,如果您使用的是MVVM,上述内容将帮助您设置Owner属性。
发布于 2015-08-03 21:11:14
您有两个选择:
1-您可以使用ShowDialog()打开子窗口,但在关闭子窗口之前,用户无法与父窗口交互。
2-您可以检查当前打开的所有窗口,方法是选中
Application.Current.Windows然后,您可以确定是否要关闭窗口
编辑:
将以下事件处理程序添加到Parent.Closing事件
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
foreach (var item in Application.Current.Windows)
{
Window window = item as Window;
if (window.Title == "YourChildWindowTitle")
{
// show some message for user to close childWindows
e.Cancel = true;
break;
}
}
}发布于 2015-08-03 21:05:06
在窗口关闭命令中,如果子窗口打开,则传递禁用关闭功能。
或
你能做的就是在弹出窗口打开并触发关闭命令时执行canexecute = false。
https://stackoverflow.com/questions/31787988
复制相似问题