13
votes

I have a WPF-Window which I don't close. Instead I do Hide() and Show() it. Now, when I doubleclick in my MainWindow in a Grid on a Record, which will Trigger to Show() the Window, the Window will always be shown behind the MainWindow. I have tried the fallowing, but with no success:

view.Show();
view.Activate();
view.Topmost = true;
view.Topmost = false;
view.Focus();       

Is there another way which I can use to bring the window absolut to the front? I cannot set the MainWindow as the Owner.

5
You're going to have to elaborate on "I cannot set the MainWindow as the Owner."vcsjones
I won't set the MainWindow as a Owner the my view because there is no relation between the Windows. And I should be able to open many of this windows.BennoDual
possible duplicate of Bring a window to the front in WPFCodeNaked
It sounds like there is a relation to the windows if window A is opening window B. window A can be the owner of multiple windows. Just don't show modally.Mranz
Why you set topmost value from true to flase Immediately?Rev

5 Answers

28
votes

Window.Activate is the way to go (If you dont want to set to owner). If this does not work (as you describe), there is an error at another location. Maybe your MainWindow has TopMost set to true ? Or you have a deferred call that focuses your main window or a control within?

Calling ShowDialog() as proposed in another answer is not an option unless you want that the whole app is blocked until the modal opened window is closed.

There is an error in the Win32-Api that influences also the window-management if WPF, but the description of your problem does not sound like this.

Additionally here a hack, but I hope that you don't need it:

Dispatcher.BeginInvoke(new Action(delegate {       
        view.Activate();
        }), System.Windows.Threading.DispatcherPriority.ContextIdle, null);
4
votes

ShowDialog instead of Show will allow the window to always be displayed on top.

3
votes

I went through a similar issue and found a solution using a combination of the other answers. Once the window is hidden I put it in the foreground with the following code :

    view.WindowState = WindowState.Normal;
    view.Activate();

Note : If the window was maximised before hiding, that code will make it come back as maximised

2
votes
myWindow.WindowState = WindowState.Normal;

That worked for me.

1
votes

I had the same problem - I was showing a window with Owner set to NULL from a MouseDoubleClick event. I realised (eventually) that I needed to set:

e.Handled = true

before my event code completed. The following Microsoft document outlines that you may want to mark an event as handled when it responds in a "significant and relatively complete way":

http://msdn.microsoft.com/en-us/library/ms747183.aspx

This is subjective, but in my case it was preventing the window I just opened from being visible to the user.