Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 256
user3719173 Avatar asked Nov 25 '14 07:11

user3719173


2 Answers

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.

like image 126
Karthik Nishanth Avatar answered Nov 08 '22 02:11

Karthik Nishanth


There is a difference between the ShowDialog() and Show() methods. ShowDialog() makes the child window active and remain active until it is closed; however, the Show() method opens a new window and treats it as a separate entity. In this case, on every click, a new window gets launched.

E.g.

Window1 childWin= new Window1();
childWin.ShowDialog();
OR
childWin.Show();
like image 34
Navin Pandit Avatar answered Nov 08 '22 04:11

Navin Pandit