TopMost is not TopMost always - WPF

wpf window always on top of parent
wpf popup always on top
wpf bring window to front
always on top not working

I have a clock application. I have set the Window's TopMost property. But, randomly, some other window or visual studio comes above clock.

Is there any other way to make my window (clock app) to display always on top of all other applications.


Are you sure it's a random window? If another window is also a topmost window, it is possible for it to come above your window.

WPF Always on Top doesn't work, TopMost is not TopMost always - WPF (4 answers). Closed 3 years ago. I'm creating an installer which has WPF front end. When I launch a  I have a clock application. I have set the Window's TopMost property. But, randomly, some other window or visual studio comes above clock. Is there any other way to make my window (clock app) to display always on top of all other applications.


I know that this question is old, but I don't quite understand why the accepted answer has received up votes... or why it was accepted... it doesn't really answer the question, or provide a solution and answers posted these days that are that short are almost always down voted and/or deleted by the community. Ah well, I guess it was posted in different times.

Either way, as old as it is, I have a possible solution for anyone who may come across this post in the future. You can simply handle the Window.Deactivated Event and/or the Application.Deactivated Event. The Window.Deactivated Event occurs when a window becomes a background window and the Application.Deactivated Event occurs when an application stops being the foreground application.

The idea is to set the relevant TopMost property to true each time your application or Window loses focus:

private void Window_Deactivated(object sender, EventArgs e)
{
    // The Window was deactivated 
    this.TopMost = true;
}

It's worth noting that other developers can also use this technique, so this doesn't guarantee that your Window will always remain on top, but it works for me and the situation is still certainly improved by using it.

Window.Topmost Property (System.Windows), Gets or sets a value that indicates whether a window appears in the topmost z-​order. Join a community of over 2.6m developers to have your questions answered on Can a floating pane NOT always be TopMost with regard to the main window? of UI for WPF Docking. New here? Start with our free trials.


I also had this problem when setting Window.Topmost = true on already existing window sometimes worked, sometimes not. Below is my workaround, you can probably combine it with Window_Deactivated approach mentioned by other people, if WS_EX_TOPMOST style is lost at runtime.

App.Current.MainWindow.Topmost = true;

// Get this window's handle
IntPtr hwnd = new WindowInteropHelper(App.Current.MainWindow).Handle;

// Intentionally do not await the result
App.Current.Dispatcher.BeginInvoke(new Action(async () => await RetrySetTopMost(hwnd)));

Extra code:

private const int RetrySetTopMostDelay = 200;
private const int RetrySetTopMostMax = 20;

// The code below will retry several times before giving up. This always worked with one retry in my tests.
private static async Task RetrySetTopMost(IntPtr hwnd)
{
    for (int i = 0; i < RetrySetTopMostMax; i++)
    { 
        await Task.Delay(RetrySetTopMostDelay);
        int winStyle = GetWindowLong(hwnd, GWL_EXSTYLE);

        if ((winStyle & WS_EX_TOPMOST) != 0)
        {
            break;
        }

        App.Current.MainWindow.Topmost = false;
        App.Current.MainWindow.Topmost = true;
    }
}

internal const int GWL_EXSTYLE = -20;
internal const int WS_EX_TOPMOST = 0x00000008;

[DllImport("user32.dll")]
internal static extern int GetWindowLong(IntPtr hwnd, int index);

T751544, Hi All, I've WPF application which has one Main window on which I'm opening a Popup window. The problem I'm facing is the Popup window. MessageBox.Show(new Form { TopMost = true }, "Hello, I'm on top!"); The code new Form { TopMost = true } will create a hidden form with the MB_TOPMOST property, which is inherited by the messagebox dialog window. As a result, it will appear on top of all your other windows.


This should do the trick in most cases

private void Window_Deactivated(object sender, EventArgs e)
{
    // The Window was deactivated 
    Topmost = false; // set topmost false first
    Topmost = true; // then set topmost true again.
}

Re: How to remain WPF Window always on top of all windows?, using System; using System.Diagnostics; using System.Windows; using System.​Runtime.InteropServices; namespace Technewlogic.Samples.WpfModalDialog  I need to run the application window as "Always on Top". The window is designed in XAML and is controled in the class "CameraView" but it does not seem to include a "TopMost" property or equivalent. Attached are the code of "CameraView.xaml.cs" and the code of "CameraView.xaml" that are part of OptiTrack SDK (NaturalPoint) called "Single_Camera


How to fix when WPF Popup control is always topmost over other , Author: Shivam Srivastava | Tuesday, November 22, 2016 | No comments |. The WPF Popup control is always "topmost" over other application windows. Please  A topmost form is a form that overlaps all the other (non-topmost) forms even if it is not the active or foreground form. Topmost forms are always displayed at the highest point in the z-order of the windows on the desktop.


MainWindow unable to be Topmost | WPF Forums, The Floating Window will always be on top of the Main. Multiple floating Even in Nested Floating Windows this is no problem. But all of these  In my main application,I'mm calling a process named A.This process is calling another application B.Problem is,the process A is always top on my main window,but when process B starts it is situated behind my main window.It is occurring not every time I am doing the operation,but sometime.


WPF: Popup that is only topmost with respect to parent window , Taken from the comments at http://chriscavanagh.wordpress.com/2008/08/13/non​-topmost-wpf-popup/ (Joe Gershgorin) which was in not easy to digest state. The WPF Popup control is always "topmost" over other application windows. Please follow below steps to fix this issue. In above code GetWindowRect is used to set current window location. TopmostProperty have bonded with event OnTopmostChanged which updates the window.