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

113,223

Solution 1

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();
    }
  }
}

Solution 2

Instead you can use a Popup that will be TopMost always, decorate it similar to a Window and to attach it completely with your Application handle the LocationChanged event of your main Window and set IsOpen property of Popup to false.

Edit:

I hope you want something like this:

    Window1 window;

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        window = new Window1();
        window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        window.Topmost = true;
        this.LocationChanged+=OnLocationchanged;
        window.Show();
    }
     
    private void OnLocationchanged(object sender, EventArgs e)
    {
          if(window!=null)
              window.Close();
    }

Hope it helps!!!

Solution 3

CustomWindow cw = new CustomWindow();

cw.Owner = Application.Current.MainWindow;

cw.ShowInTaskbar = false;

cw.ShowDialog() ; 

Solution 4

Simple to do it in XAML, and surprised that nobody posted this answer yet. In the following example, the Window is defined in a ResourceLibrary (notice the x:Key), but you can also use this XAML binding on a standalone Page-style WPF resource.

<Window x:Key="other_window" 
        Topmost="{Binding Source={x:Static Application.Current},Path=MainWindow.IsActive,Mode=OneWay}">
    <TextBlock Text="OTHER WINDOW" />
</Window>

Solution 5

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

Share:
113,223
Sylvain
Author by

Sylvain

Software Architect at http://www.octopus-itsm.com

Updated on November 13, 2020

Comments

  • Sylvain
    Sylvain over 3 years

    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 all applications and I don't want that.

  • Sylvain
    Sylvain about 14 years
    That question is for "Windows Forms" Windows. I have WPF Window.
  • Sylvain
    Sylvain about 14 years
    I don't want a modal window. Just a top level window.
  • Sylvain
    Sylvain about 14 years
    I can't set the Owner property because in my case, the window is created before any other windows. Therefore, I don't have any other window to set as the owner.
  • paradisonoir
    paradisonoir about 14 years
    so why not just use the WindowStartupLocation="CenterScreen"
  • Sylvain
    Sylvain about 14 years
    Because if the user click another windows that is behind, it will not stay on top. I want it to be always on top but I don't want it to block the other windows (that is what modal do).
  • Sylvain
    Sylvain about 14 years
    By the way, WindowStartupLocation only affects the initial values for the Top and Left properties of the window when it is opened; it does not affect the Z order.
  • paradisonoir
    paradisonoir about 14 years
    you're right about the WindowStartupLocation. Though, I don't understand the usability of having a window on top of every other windows and still not blocking them. Would you please elaborate more about it? Do you want to have this window on top, but still be able to change something in other windows?
  • Sylvain
    Sylvain about 14 years
    In general, tool windows work like that. For instance, you can undock the "Tools" toolbar in Visual Studio and float it on top of your code window. You can still type in the code windows even if that window is on top. If you bring Notepad on top of Visual Studio, Notepad will hide the Tools window. Therefore that windows is on top of all windows in Visual Studio but not on top of all the windows of the system.
  • Matt Wilko
    Matt Wilko over 12 years
    Can you provide some explanation along with your code - this helps everyone to understand your answer
  • Alex
    Alex about 12 years
    If you configure window so, it will behave as desired. Window will be on top of all other windows in application
  • Colonel Panic
    Colonel Panic almost 12 years
    Didn't work for me "null reference exception". My dialog is a WPF Window but the application and main window are WinForms.
  • Holf
    Holf almost 11 years
    There are ways to do this. Some of the answers given are good candidates.
  • rob_williams
    rob_williams over 10 years
    This works flawlessly for making popup or utility windows that don't hover over other applications' windows when not active. Thanks!
  • doobop
    doobop about 10 years
    This is going to make it modal. Want the secondary window to be in front of the main window, but be able to interact with the main window.
  • N t
    N t about 10 years
    This is exactly what I was looking for - how to open a new window that retains focus/activity until closed. Thank you.
  • Matt Zappitello
    Matt Zappitello about 10 years
    When I click on another application, my window is no longer active, so Topmost never gets set to false.
  • Matt Zappitello
    Matt Zappitello about 10 years
    I was able to get this to work by check Topmost in the if statement, rather than IsActive: if (ownedWindow.Topmost)...
  • mkb
    mkb about 8 years
    Better to add myOwnedDialog.ShowInTaskbar = false; in to NewWindowAsDialog
  • Yoav Feuerstein
    Yoav Feuerstein almost 7 years
    what if cw already existed, and you wanted this to be under it?
  • Martin Schneider
    Martin Schneider almost 6 years
    This makes the window on top of all other applications as well. And what is the meaning of the LocationChanged handler (btw: handlers are not prefixed with 'On' by C# convention)?
  • Burak Yeniçeri
    Burak Yeniçeri about 5 years
    So, explain me how paint.net made the color, history and toolbox windows that is only on top of the application?
  • Sylvain
    Sylvain about 5 years
    @BurakYeniçeri 9 years later....I don't know. Back then I concluded that this was not possible after a lot of attempts. Much later Johannes came up with a solution that has a lot of upvotes, I suppose it's a good solution. I've cleared the "accept" on my own answer.
  • Paweł Iwaneczko
    Paweł Iwaneczko over 2 years
    After setting Owner you can use Show only. Main window is clickable then