Reputation: 11
I am working on WPF application where I am running a scan function in the main window. 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
Upvotes: 0
Views: 5611
Reputation: 1771
Set parent window as owner for child window (Popup) and open your child window as Showdialog()
like below:
Create "NavigationService.cs
" class and put the below code in this class.
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:
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.
Upvotes: 2
Reputation: 1668
Try to use current Dispatcher
object for calling 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.
}));
Upvotes: 0
Reputation: 69979
I believe that reading the Dialog Boxes Overview page on MSDN will clear up your confusion. 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
.
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.
Upvotes: 0