WPF disable main window while second window is open until it closed

走远了吗. 提交于 2019-12-08 15:04:58

问题


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.


回答1:


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

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

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

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

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

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

For that inside the Click handler, ....

var newWindow = new Window1();
newWindow.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.



来源:https://stackoverflow.com/questions/27121419/wpf-disable-main-window-while-second-window-is-open-until-it-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!