简体   繁体   English

禁用主窗口WPF

[英]Disable Main Window WPF

I am working on WPF application where I am running a scan function in the main window. 我正在WPF应用程序上工作,我在主窗口中运行扫描功能。 The scan section is running in a separate thread. 扫描部分在单独的线程中运行。 After scan terminates, it shows another window as a modal popup. 扫描终止后,它将另一个窗口显示为模式弹出窗口。

Now, What I exactly want is to disable the main window when the popup comes out after the scan gets completed. 现在,我真正想要的是在扫描完成后弹出弹出窗口时禁用主窗口。 So, the user cannot click on the main window until he closes the popup. 因此,用户只有在关闭弹出窗口后才能在主窗口上单击。 But I am unable to do that. 但是我做不到。

Thanks 谢谢

Set parent window as owner for child window (Popup) and open your child window as Showdialog() like below: 将父窗口设置为子窗口(Popup)的所有者,并以Showdialog()打开子窗口,如下所示:

Create " NavigationService.cs " class and put the below code in this class. 创建“ NavigationService.cs ”类,并将以下代码放入此类。

public void ShowPopUpWindow(PopUpWindowViewModel popUpWindowViewModel)
       {
           PopUpWindowView= new PopUpWindowView();
           PopUpWindowView.DataContext = popUpWindowViewModel;
           PopUpWindowView.Owner = ParentWindowView;
           PopUpWindowView.ShowDialog();
       }

Now, call the above method in your ViewModel class like below: 现在,在ViewModel类中调用上述方法,如下所示:

PopUpWindowViewModel popUpWindowViewModel = new PopUpWindowViewModel ();
PopUpWindowViewModel.Name = "This is Popup Window";
NavigationService navigationService = new NavigationService();
navigationService.ShowPopUpWindow(PopUpWindowViewModel);

It will not let you to click on parent window until you close the child window. 在关闭子窗口之前,它不会让您单击父窗口。

I believe that reading the Dialog Boxes Overview page on MSDN will clear up your confusion. 我相信阅读MSDN上的“ 对话框概述”页面将消除您的困惑。 From the linked page: 从链接页面:

A modal dialog box is displayed by a function when the function needs additional data from a user to continue. 当功能需要用户提供其他数据以继续时,该功能将显示一个模式对话框。 Because the function depends on the modal dialog box to gather data, the modal dialog box also prevents a user from activating other windows in the application while it remains open. 因为该功能依赖于模式对话框来收集数据,所以模式对话框还阻止用户在应用程序保持打开状态的同时激活其他窗口。 In most cases, a modal dialog box allows a user to signal when they have finished with the modal dialog box by pressing either an OK or Cancel button. 在大多数情况下,模态对话框允许用户通过按“确定”或“取消”按钮来完成模态对话框的信号。 Pressing the OK button indicates that a user has entered data and wants the function to continue processing with that data. 按下“确定”按钮表示用户已输入数据,并希望功能继续使用该数据进行处理。 Pressing the Cancel button indicates that a user wants to stop the function from executing altogether. 按下取消按钮表示用户希望完全停止执行该功能。 The most common examples of modal dialog boxes are shown to open, save, and print data. 模态对话框的最常见示例显示为打开,保存和打印数据。

Please read the Creating a Modal Custom Dialog Box section on the linked page to find out how to create your own custom modal dialog Window . 请阅读链接页面上的“ 创建模态定制对话框”部分,以了解如何创建自己的定制模态对话框Window


UPDATE >>> 更新>>>

A very simple alternative is to add an element that will overlay all other elements on the page: 一个非常简单的替代方法是添加一个元素,该元素将覆盖页面上的所有其他元素:

<Grid>
    <!-- Your content here -->  
    <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
         Fill="#3FFFFFFF" Visibility="{Binding IsOverlayVisible, 
         Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Grid>

Of course, you'll need to data bind a bool property ( IsOverlayVisible ) using a IValueConverter to control when it's displayed. 当然,您需要使用IValueConverter来控制显示bool属性( IsOverlayVisible )的IValueConverter以控制其显示时间。

Try to use current Dispatcher object for calling ShowDialog . 尝试使用当前的Dispatcher对象调用ShowDialog

use following block in your code which you are executing in some other thread. 在其他线程中执行的代码中使用以下代码块。

Application.Current.Dispatcher.Invoke(new Action(() =>
{
    //Show your dialog here.

}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM