Modal Dialog not showing on top of other windows

前端 未结 5 1998
无人及你
无人及你 2020-12-05 23:43

I am using Window.ShowDialog() to open a modal window in my WPF (MVVM) application, but it lets me navigate to other windows using the Windows taskbar (Windows

相关标签:
5条回答
  • 2020-12-05 23:48

    There isn't any code to base this off of, but it sounds like you have left off some properties on the Window you've created and expected ShowDialog to apply additional "dialog" semantics:

    Window window = new Window()
    {
        Title = "Modal Dialog",
        ShowInTaskbar = false,               // don't show the dialog on the taskbar
        Topmost = true,                      // ensure we're Always On Top
        ResizeMode = ResizeMode.NoResize,    // remove excess caption bar buttons
        Owner = Application.Current.MainWindow,
    };
    
    window.ShowDialog();
    
    0 讨论(0)
  • 2020-12-05 23:48

    I ended up using a combination of a couple of the answers here. The accepted answer was useful at first but as other people on here have pointed out setting Topmost = true means that the window is always above any other applications running. My solution was this:

    var myWindow = new MyWindowType();
    myWindow.Owner = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
    

    I initially used:

    myWindow.Owner = Application.Current.MainWindow;
    

    However, this method causes problems if you have three windows open like this:

    MainWindow
       |
       -----> ChildWindow1
    
                   |
                   ----->  ChildWindow2
    

    Then setting ChildWindow2.Owner = Application.Current.MainWindow will set the owner of the window to be its grandparent window, not parent window.

    To speed things up, I've added it as a code snippet in Visual Studio. If you add the following to Tools --> Code Snippet Manager --> My Code Snippets:

    <CodeSnippets
        xmlns="http://schemas.microsoft.com/VisualStudio/2010/CodeSnippet">
      <CodeSnippet Format="1.0.0">
        <Header>
          <Title>MVVM Set owner of page to be current active window</Title>
          <Shortcut>owner</Shortcut>
        </Header>
        <Snippet>
          <Code Language="CSharp">
            <![CDATA[System.Windows.Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);]]>
          </Code>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>
    

    Typing 'owner' and double-tapping the tab key will add the 'Application.CurrentWindows...' part in for you automatically.

    0 讨论(0)
  • 2020-12-05 23:54

    Just set the owner property of the Window to the calling window. Then the activation of the WPF application in taskbar not just activates the MainWindow but also the modal child window and brings it to the front.

    ChildWindow C = new ChildWindow();
    C.Owner = this;
    C.ShowDialog();
    
    0 讨论(0)
  • 2020-12-05 23:58

    Setting Topmost to True (Topmost=True) causes the dialog to be on the top of all windows in the system (not only in application).

    So you can try to register the event Activated in your main window:

    Activated += WindowActivated;
    

    and activate an owner window of the modal dialog every time when another main window of your application become active.

    private void WindowActivated(object sender, EventArgs e)
    {
        Window window = Application.Current.Windows.OfType<YourMainWindow>().FirstOrDefault(p => p != this && !p.IsActive && p.OwnedWindows.Count > 0);
        if (window != null)
        {
            window.Activate();
        }
    }
    

    This simple hack assumes all your children windows are modal, but you can write a more sophisticated logic.

    0 讨论(0)
  • 2020-12-06 00:14

    Had to do a bit of modification. I had to set the owner and activate the window. Check for the pop up window and activate the window as given below.

            var enumerator = Application.Current.Windows.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Window window = (Window)enumerator.Current;
                if (window != null && window.GetType() == typeof(PopUpWindow))
                {
                    window.Activate();
                }
            }
    
    0 讨论(0)
提交回复
热议问题