Keven M
Keven M

Reputation: 992

Bring parent window to front when child window selected from taskbar

The title pretty much says it all, but basically I have a main parent window, which occasionally opens child windows. Right now it's possible to select one of the child windows from the Windows 7 taskbar, and only that window will be brought to the front. What I'd like it to find a way to link the parent window to this command, so that any time a child window is selected the parent is automatically brought to the front as well.

I tried to use both the Focus() and Topmost = true commands from within the child windows 'GotFocusevent handler, but neither seemed to make a difference. I also tried theBringIntoView()` method, but again, no joy. Has anyone seen this before or know a way to implement this?

This is what I've tried so far. The logic in setting mainWindow first and then immediately setting the child window is that I do still want the child window to have focus, but I want mainWindow to be above any other programs running (ie-Excel, VS, etc).

    private void Window_GotFocus(object sender, RoutedEventArgs e)
    {
        var mainWindow = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;

        mainWindow.Topmost = true;
        this.Topmost = true;

    }

Upvotes: 0

Views: 2874

Answers (1)

Dan Busha
Dan Busha

Reputation: 3803

I think you're listening to the wrong event to be notified when your window is selected. Subscribe to the Activated event on your child window which should let you know when your window is selected in the taskbar. From there you can Activate() your MainWindow.

Additionally, I think if you set the parent window to be the Owner of the child window you'll get this behavior automatically.

Upvotes: 2

Related Questions