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.
c # wpf modal-dialog
Dirk vollmar
source share