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

后端 未结 19 3018
南笙
南笙 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 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();
        }
      }
    }
    

提交回复
热议问题