WPF 在第二个窗口打开时禁用主窗口,直到它关闭 [英] WPF disable main window while second window is open until it closed

查看:39
本文介绍了WPF 在第二个窗口打开时禁用主窗口,直到它关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 应用程序,它有一个主窗口和一个可以通过主窗口中的按钮打开的第二个窗口.我希望主窗口被禁用,而第二个窗口在 Visual Studio 中作为关于"窗口打开.

I have a WPF application with a main window and a second window that can be opened from a button in the main window. I want the main window to be disabled while the second window is open as an "About" window in Visual Studio.

推荐答案

试试这个 ShowDialog 方法 而不是 Show 将第二个窗口作为对话框打开.

Try this ShowDialog method instead of Show to open the second window as a dialog.

  1. 您已经有一个带有窗口的 WPF 项目.这个应用应该可以运行.

  1. You have a WPF project already with a window. This app should work.

右键单击项目并添加新窗口.您将其命名为 Window1.xaml

Right click on project and Add new Window. You name it Window1.xaml

您现在会注意到 Window1.xaml 和 Window1.xaml.cs 添加到您的项目中.(窗口的类名是 Window1,它位于 .xaml.cs 文件中,它派生自 Window;也是一个部分类)

You would now notice Window1.xaml and Window1.xaml.cs added to your project. (the class name for the window would be Window1 which is in the .xaml.cs file and it derives from Window; also a partial class)

打开 Window1 (Window1.xaml) 的 XAML 文件并添加控件.像对待任何其他窗口一样对待它并编写代码.

Open the XAML file for the Window1 (Window1.xaml) and add your controls. Treat it like any other window and write code.

现在在您的主窗口(第一个)中添加一个按钮,单击该按钮时应显示新创建的窗口.

Now in your main window (the first one) you add a Button which when clicked should show the newly created window.

对于 Click 处理程序内部的那个,....

For that inside the Click handler, ....

var newWindow = new Window1();
newWindow.ShowDialog();

这个 Window1 应该是你关于页面的设计.使用 ShowDialog(); 调用它会禁用其他窗口,唯一的活动窗口将是您的关于页面.

This Window1 should be the design for your About page. Invoking it with ShowDialog(); disables the other windows and the only active window will be your about page.

这篇关于WPF 在第二个窗口打开时禁用主窗口,直到它关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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