Refer to active Window in WPF?

pkain picture pkain · Jan 10, 2010 · Viewed 50.4k times · Source

How can I refer to active Window of WPF application in C#, using something like ActiveForm property in WinForms?

Answer

Aviad P. picture Aviad P. · Jan 10, 2010

One possible way would be to scan the list of open windows in the application and check which one of them has IsActive = true:

Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);

Not sure if there may be more than one active window if, for example, there's a modal dialog showing, in which case, the owner of the dialog and the dialog itself might be active.