HCL
HCL

Reputation: 36765

How do bring a WPF Window to front?

I have created a single-instance application and want to activate an already opened window if the user starts the app multiple times. This works fine however I have the problem, that if the already opened window is beyond another applications window, I must bring it to front.

I have tried window.Focus() and window.Show() but both of them seem not to work. As a workaround I use …

bool oldTopMost = window.Topmost;
window.Topmost = true;
window.Topmost = oldTopMost;
window.Focus();

… this does the job but looks to me very ugly. Has anyone a better solution for this?

Upvotes: 18

Views: 13524

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564333

You could use Window.Activate instead:

window.Activate();

This is the WPF equivelent to calling SetForegroundWindow.

Upvotes: 31

Related Questions