Search code examples
c#wpfshowdialog

Open and close 2 windows from another window in a right sequence


I have a WPF application. It has 3 windows i need to open and close in RIGHT sequence: Worker Window, Request Window, and User Messenger Window, let's name them Window1, Window2, Window3 for simplicity. Window2 has a messenger button, that opens Window3. But there is another button in Window1, that will open both Window2 and Window3. What exactly do i want to achieve:

  1. Click on a button in Window1
  2. Window2 and Window3 are opened, with following z-indexes: Window1 - 1; Window2 - 2; Window3 - 3
  3. While Window3 is opened, no actions can be done in other windows (how ShowDialog() works), same with Window2 (assuming Window3 is closed)
  4. Whenever window is closed, they KEEP their z-indexes

What i tried: My initial code:

                var rw = new RequestWindow(Request, false);
                var umw = new UserMessengerWindow(Request);
                rw.Show();
                umw.Topmost = true;
                umw.ShowDialog();

What really happens: it opens those windows, but Whenever Window3 is closed, Window2 goes to the back (is behind Window1). I tried setting owner property:

                var rw = new RequestWindow(Request, false);
                var umw = new UserMessengerWindow(Request);
                rw.Owner = Application.Current.Windows.OfType<WorkerWindow>().FirstOrDefault();
                rw.Show();
                umw.Topmost = true;
                umw.ShowDialog();

Now whenever Window3 is closed, Window1 and Window2 keep their original z-indexes, but focus goes to Window1 (and it is interactable, while i want it to be not), using Focus() or Activate() on Window2 doesn't help.


Solution

  • If I understand correctly what you need:

    <Window x:Class="Core2024.SO.JamalShakhmurzaev.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Core2024.SO.JamalShakhmurzaev"
            mc:Ignorable="d"
            Title="Window1" Height="450" Width="800">
        <UniformGrid Columns="1" Background="Yellow">
            <Viewbox>
                <TextBlock Text="First" Margin="5"/>
            </Viewbox>
            <Viewbox>
                <Button Content="Open Second Window" Margin="5" Padding="15 5"
                        Click="OnOpenSecondWindow"/>
            </Viewbox>
             <Viewbox>
                <Button Content="Open Third Window" Margin="5" Padding="15 5"
                        Click="OnOpenThirdWindow"/>
            </Viewbox>
       </UniformGrid>
        <x:Code>
            <![CDATA[
            private void OnOpenSecondWindow(object sender, RoutedEventArgs e)
            {
                Window2 window2 = new Window2();
                window2.Owner = this;
                window2.ShowDialog();
            }
    
            private void OnOpenThirdWindow(object sender, RoutedEventArgs e)
            {
                Window2 window2 = new Window2();
                window2.Owner = this;
                window2.Loaded += delegate
                {
                    Window3 window3 = new Window3();
                    window3.Owner = window2;
                    window3.ShowDialog();
                };
                window2.ShowDialog();
            }
            ]]>
        </x:Code>
    </Window>
    
    <Window x:Class="Core2024.SO.JamalShakhmurzaev.Window2"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Core2024.SO.JamalShakhmurzaev"
            mc:Ignorable="d"
            Title="Window2" Height="450" Width="800">
        <UniformGrid Columns="1" Background="LightGreen">
            <Viewbox>
                <TextBlock Text="Second" Margin="5"/>
            </Viewbox>
            <Viewbox>
                <Button Content="Open Third Window" Margin="5" Padding="15 5"
                        Click="OnOpenThirdWindow"/>
            </Viewbox>
        </UniformGrid>
        <x:Code>
            <![CDATA[
            private void OnOpenThirdWindow(object sender, RoutedEventArgs e)
            {
                Window3 window3 = new Window3();
                window3.Owner = this;
                window3.ShowDialog();
            }
            ]]>
        </x:Code>
    </Window>