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

后端 未结 19 2752
南笙
南笙 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.
    0 讨论(0)
  • 2020-12-14 06:01

    Try this:

    Popup.PlacementTarget = sender as UIElement;
    
    0 讨论(0)
  • 2020-12-14 06:01

    I just ran into this same issue. I have a desktop app that has multiple WPF windows, and I needed my custom splash screen to be on top of the other windows in my app only. No other windows are open when my splash screen comes up, but I do open the MainWindow from my splash screen after some authentication. So I just did something similar to what @GlenSlayden did but in code behind since, like I said, the MainWindow isn't up for me to bind to:

    private void SplashScreen_ContentRendered(object sender, EventArgs e)
    {
        // User authentication...
        // ...
    
        MainWindow mainWindow = new MainWindow();
        SetBinding(SplashScreen.TopmostProperty, new Binding("IsVisible"))
        {
            Source = mainWindow,
            Mode = BindingMode.OneWay,
            UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
        };
        mainWindow.Show();
    }
    

    Now while my program is loading all the other windows from the MainWindow, the splash screen is on top, but while the program is authenticating the user, it's not topmost, so you can click away on some other program and it will hide behind it. It's the closest thing I could find to a solution for this problem. It's not perfect because it still goes over top of all other applications while my program is loading after authentication, but that's not for very long in my case.

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

    You need to set the owner property of the window.

    You can show a window via showdialog in order to block your main window, or you can show it normal and have it ontop of the owner without blocking the owner.

    here is a codeexample of the codebehind part - I left out all obvious stuff:

    namespace StackoverflowExample
    {
      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
        }
        void NewWindowAsDialog(object sender, RoutedEventArgs e)
        {
          Window myOwnedDialog = new Window();
          myOwnedDialog.Owner = this;
          myOwnedDialog.ShowDialog();
        }
        void NormalNewWindow(object sender, RoutedEventArgs e)
        {
          Window myOwnedWindow = new Window();
          myOwnedWindow.Owner = this;
          myOwnedWindow.Show();
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-14 06:08

    I'm the OP. After some research and testing, the answer is:

    No, there is no way to do exactly that.

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

    In the popup window, overloads the method Show() with a parameter:

    Public Overloads Sub Show(Caller As Window)
        Me.Owner = Caller
        MyBase.Show()
    End Sub
    

    Then in the Main window, call your overloaded method Show():

    Dim Popup As PopupWindow
    
    Popup = New PopupWindow
    Popup.Show(Me)
    
    0 讨论(0)
提交回复
热议问题