How to make a WPF window be on top of all other windows of my app (not system wide)?

后端 未结 19 2750
南笙
南笙 2020-12-14 05:42

I want my window to be on top of all other windows in my application only. If I set the TopMost property of a window, it becomes on top of all windows of al

相关标签:
19条回答
  • 2020-12-14 05:48

    I just ran into the same problem and found trouble setting the owner using MVVM without causing the app to crash in production. I have a Window Manager View Model that includes a command to open a window using the uri of the window - and I wasn't able to set the owner to App.MainWindow without the app crashing.

    So - Instead of setting the owner, I bound the TopMost property of the window to a property in my Window Manager View model which indicates whether the application is currently active. If the application is active, the window is on top as I would like. If it is not active, other windows can cover it.

    Here is what I added to my View Model:

     public class WindowManagerVM : GalaSoft.MvvmLight.ViewModelBase
        {
            public WindowManagerVM()
            {
                App.Current.Activated += (s, e) => IsAppActive = true;
                App.Current.Deactivated += (s, e) => IsAppActive = false;
            }
    
            private bool _isAppActive = true;
            public bool IsAppActive
            {
                get => _isAppActive;
                set
                {
                    if (_isAppActive != value)
                    {
                        _isAppActive = value;
                        RaisePropertyChanged(() => IsAppActive);
                    }
                }
            }
        }
    

    Here is the XAML that implements it (I use MVVM light with a ViewModelLocator as a static resource in my app called Locator):

    <Window Topmost="{Binding WindowManager.IsAppActive, Source={StaticResource Locator}}"/>
    
    0 讨论(0)
  • 2020-12-14 05:49

    use the Activate() method. This attempts to bring the window to the foreground and activate it. e.g. Window wnd = new xyz(); wnd.Activate();

    0 讨论(0)
  • 2020-12-14 05:50

    I too faced the same problem and followed Google to this question. Recently I found the following worked for me.

    CustomWindow cw = new CustomWindow();
    cw.Owner = this;
    cw.ShowDialog();
    
    0 讨论(0)
  • 2020-12-14 05:52

    Here's a way to do it: make your "topmost" window subscribe to your other windows GotFocus and LostFocus events and use the following as the event handlers:

    class TopMostWindow
    {
        void OtherWindow_LostFocus(object sender, EventArgs e)
        {
            this.Topmost = false;
        }
    
        void OtherWindow_GotFocus(object sender, EventArgs e)
        {
            this.Topmost = true;
        }
    }
    
    0 讨论(0)
  • 2020-12-14 05:52

    How about htis:

    Private Sub ArrangeWindows(Order As Window())
        For I As Integer = 1 To Order.Length -1
            Order(I).Owner = Order(I - 1)
        Next
    End Sub
    
    0 讨论(0)
  • 2020-12-14 05:52

    This is what helped me:

    Window selector = new Window ();
    selector.Show();
    selector.Activate();
    selector.Topmost = true;
    
    0 讨论(0)
提交回复
热议问题