Modal Dialog not showing on top of other windows

前端 未结 5 2014
无人及你
无人及你 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: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().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.

提交回复
热议问题