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

后端 未结 19 3048
南笙
南笙 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:59

    The best way is set this two events to all of windows of your app:

    GotKeyboardFocus
    LostKeyboardFocus
    

    in this way:

    WiondowOfMyApp_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
    {
        windowThatShouldBeTopMost.TopMost = true;
    }
    
    WiondowOfMyApp_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
    {
        windowThatShouldBeTopMost.TopMost = false;
    }
    
    • and surely all of the windows that you wanted to be top, should be accessible from other windows of your app. in my case I have a base window and another some windows that should be top of my base window, so this was not bad to my base window has instance of each another windows.

提交回复
热议问题