Search code examples
c#wpfwindow

Refer to active Window in WPF?


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


Solution

  • 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.