tags:

views:

674

answers:

5

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.

+1  A: 

There are several threads, there's even a "topmost" tag. Search on that, or go directly to this post which looks good:

http://stackoverflow.com/questions/1393260/how-to-keep-a-window-on-top-of-all-other-windows-in-my-application-only

Chris Thornton
That question is for "Windows Forms" Windows. I have WPF Window.
Sly
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.
Sly
+1  A: 

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!!!

viky
Popup is a Control and cannot be used as a standalone window. I need is standalone window.
Sly
@Sly I have updated my answer
viky
A: 

You can add this to your windows tags

WindowStartupLocation="CenterScreen"

Then you can also display it if you want your users to acknowledge it in order to proceed

YourWindow.ShowDialog();

First try it without TopMost parameters and see the results.

paradisonoir
I don't want a modal window. Just a top level window.
Sly
so why not just use the WindowStartupLocation="CenterScreen"
paradisonoir
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).
Sly
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.
Sly
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?
paradisonoir
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.
Sly
A: 

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

No, there is no way to do exactly that.

Sly
A: 

Here's a way to do it: make your "topmost" window subscribe to your other windows GotFocus and LostFocus events and use the following as the event handlers:

class TopMostWindow
{
    void OtherWindow_LostFocus(object sender, EventArgs e)
    {
        this.Topmost = false;
    }

    void OtherWindow_GotFocus(object sender, EventArgs e)
    {
        this.Topmost = true;
    }
}
Juan Campa