How can I refer to active Window of WPF application in C#, using something like ActiveForm property in WinForms?
5 Answers
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.
-
2Looking at my old answer again, it might be better to use the
OfType<Window>()
operator instead of theCast<Window>()
one just in case...– Aviad P.Jun 20, 2012 at 8:00 -
awesome! This keeps me from having to pass a reference to the window, which keeps my data structure free of UI references. thanks! Mar 7, 2013 at 22:19
-
6To make it more robust, you could as well use
FirstOrDefault
instead ofSingleOrDefault
which throws an exception if there are multiple matching items. Plus it should be a tiny bit faster because it accepts the first result and doesn't need to check that it's the only one.– ygoeOct 25, 2013 at 15:04 -
1Sometimes both
FirstOrDefault
andSingleOrDefault
returnsnull
, meaning there are no windows with IsActive as true. How is that possible?– digitguyApr 8, 2014 at 8:24 -
1
There is better way to do this using PInvoke. Aviads answer is not working all the time (there are some edge cases with dialogs).
IntPtr active = GetActiveWindow();
ActiveWindow = Application.Current.Windows.OfType<Window>()
.SingleOrDefault(window => new WindowInteropHelper(window).Handle == active);
One must include following import first:
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
-
1Hehe. It took me few hours to discover this myself. I actually wrote the exact same thing as you & come here to share it, but you were first, so here's my vote :P Jan 15, 2013 at 15:04
-
6
-
1@nchaud I am using AvalonDock which allows you to drag a workspace out of the main window and into a new window. However, these windows are also marked
IsActive
. Using the other solution threw an exception (SingleOrDefault
throws if there are more than one matching the predicate) or didn't give me the actual active window when usingFirstOrDefault
– clctoAug 7, 2014 at 21:06
I know this is a bit old question but I think my answer could help someone.
My problem was this: I had a WPF MVVM application and I needed to get my MainWindow
instance in the second view, i.e. second view model, in order to set the visibility of title bar button to visible
.
This is my solution:
MainWindow window = (MyApp.MainWindow)App.Current.MainWindow;
window.btnSearch.Visibility = System.Windows.Visibility.Visible;
Hope this would help someone.
I have problems With this way "Application.Current.Windows.OfType().SingleOrDefault(x => x.IsActive);" specialy because I was building an aplication with a main Window then i had problems when the main window was selected. I resolve it creating this:
In some base class or App.xaml.cs create this:
public static Window ActivatedWindow {get;set;}
Then put in your base class deriving Window or all of your Window's Activate Event:
First Option - personal Window Base Class:
public class MetroToolWindowBase
{
public MetroToolWindowBase()
{
Activated += new EventHandler(MakeActive);
}
private void MakeActive(object sender, EventArgs e)
{
App.ActivatedWindow= this;
}
}
Second Option- In Activated Event of Windows:
private void XWindow_Activated(object sender,EventArgs e)
{
App.ActivatedWindow= this;
}
Another way to do it is to use the native GetActiveWindow
function from user32.dll.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetActiveWindow();
To convert it to an actual WPF Window:
IntPtr handle = GetActiveWindow();
HwndSource hwndSource = HwndSource.FromHwnd(handle);
var window = hwndSource?.RootVisual as Window;
If hosting a WPF Window in a WinForms app, WindowInteropHelper
should be used. This makes for example the Window owner work correctly:
var wih = new WindowInteropHelper(window)
{
Owner = GetActiveWindow()
};
I edited my old answer because the edge case I encountered disappeared after a Visual Studio update, but it can be checked from answer history. I encountered an issue there where I was getting null for active window in certain circumstances while debugging.