13

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.

Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
BennoDual
  • 5,865
  • 15
  • 67
  • 153
  • 1
    You're going to have to elaborate on "I cannot set the MainWindow as the Owner." – vcsjones Aug 26 '11 at 23:38
  • 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 Aug 26 '11 at 23:57
  • 2
    possible duplicate of [Bring a window to the front in WPF](http://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf) – CodeNaked Aug 27 '11 at 00:09
  • 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 Aug 27 '11 at 00:56
  • Why you set `topmost value` from true to flase Immediately? – Rev Aug 27 '11 at 03:52

4 Answers4

28

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);
Community
  • 1
  • 1
HCL
  • 36,053
  • 27
  • 163
  • 213
3

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

leguminator
  • 164
  • 1
  • 8
  • This worked for me, and just straight Activate did not. Thanks very much for the suggestion! – ravuya Dec 07 '16 at 20:21
  • 1
    Doing this even if the window starts as maximized it will put it with the default height/width – Camadas Sep 20 '18 at 09:48
2
myWindow.WindowState = WindowState.Normal;

That worked for me.

Serge
  • 21
  • 1
1

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.

Mitkins
  • 4,031
  • 3
  • 40
  • 77