Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Bring Window to Front

Tags:

c#

.net

wpf

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.

like image 771
BennoDual Avatar asked Aug 26 '11 23:08

BennoDual


4 Answers

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

like image 72
leguminator Avatar answered Oct 24 '22 10:10

leguminator


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);
like image 39
HCL Avatar answered Oct 24 '22 10:10

HCL


myWindow.WindowState = WindowState.Normal;

That worked for me.

like image 2
Serge Avatar answered Oct 24 '22 10:10

Serge


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.

like image 1
Mitkins Avatar answered Oct 24 '22 10:10

Mitkins