How to implement a modal dialogue on top of a modeless dialogue?

In a WPF application, I would like to implement the following behavior, which does not seem to work simply:

In the main window ( Window1 ), the user opens a modeless window ( Window2 ), and this modeless window can display a modal dialog ( Window3 ).

The problem is that whenever a modal dialog is displayed, the main window disappears in the background (provided that windows of other applications open) when the user closes the dialogs.

Is there something wrong with the way I use Window.Owner and Window.Show() / Window.ShowDialog() , is this a bug or is something just not supported?

The following simple WPF application demonstrates this behavior:

 public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { Window2 win = new Window2(); win.Owner = this; win.Show(); } } public partial class Window2 : Window { public Window2() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { Window3 win = new Window3(); win.Owner = this; win.ShowDialog(); } private void btnClose_Click(object sender, RoutedEventArgs e) { this.Close(); } } public partial class Window3 : Window { public Window3() { InitializeComponent(); } private void btnClose_Click(object sender, RoutedEventArgs e) { this.Close(); } } 

XAML Window1 :

 <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1"> <Button Click="Button_Click">Show non-modal window</Button> </Window> 

XAML Window2 :

 <Window x:Class="WpfApplication1.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window2"> <StackPanel> <Button Click="Button_Click">Show modal dialog</Button> <Button Name="btnClose" Click="btnClose_Click">Close</Button> </StackPanel> </Window> 

XAML Window3 :

 <Window x:Class="WpfApplication1.Window3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window3"> <Button Name="btnClose" Click="btnClose_Click">Close</Button> </Window> 

UPDATE: Fixed error copying and pasting into code. This is .NET 3.5 SP1, if that matters.

+6
c # wpf modal-dialog
source share
1 answer

Microsoft confirms this as a bug in WPF:

This is not a regression from previous releases, so it does not force the panel to commit for this version of the product. We will consider this for a future version.

At the same time, this can be circumvented by activating the owner window when closing the child window.

Code example:

 public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void NonModalButtonClick(object sender, RoutedEventArgs e) { new Window1 { Owner = this }.Show(); } private void ModalButtonClick(object sender, RoutedEventArgs e) { new Window1 { Owner = this }.ShowDialog(); } protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { if (this.Owner != null) { this.Owner.Activate(); } } } 

(Note that the workaround always brings the main window to the forefront, which may differ from the expected behavior)

+8
source share

All Articles