Bringing Window to front in WPF

Recently I was working on WPF application where the Window would be minimized to tray and when user click’s the tray icon, the window has to show up in front of other windows.

I tried lot many variants and had trouble with Windows 7 and Windows 2008 R2. The following code finally worked for me.

Note: This code was present in the Window which was supposed to be controlled. If required replace “this” with actual reference of the Window.

if (!this.IsVisible)
{
    this.Show();
}
this.WindowState = WindowState.Normal;
this.Activate();
this.Topmost = true;
this.Topmost = false;
this.Focus();