Will
Will

Reputation: 165

C# wpf multiple windows on task bar

I created a C# project which have multiple windows. When I do some other stuff, some of the windows are lost focus. Then I have to bring them to front by clicking the task bar one by one.

I am wondering is there are OnTaskBarClick Event on C# wpf window?

Then What I need to do is to set all sub windows with "ShowInTaskbar="False"". Then when ever my mainwindow is clicked on the task bar, I bring all the windows to the front.

Upvotes: 0

Views: 1307

Answers (2)

John Wright
John Wright

Reputation: 4607

You'll want to use the Window.Activated event to detect when your application's window is brought into focus:

From the documentation:

A window is activated (becomes the foreground window) when:

  • The window is first opened.

  • A user switches to a window by selecting it with the mouse, pressing ALT+TAB, or from Task Manager.

  • A user clicks the window's taskbar button.

Or you could use the Application.Activated event.

Clicking on a control of an already active window can also cause the Window.Activated event to fire, so if the issue is that it's firing too often, you'll probably want to watch for when the application toggles between being active and deactive.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Application.Current.Activated += CurrentOnActivated;
        Application.Current.Deactivated += CurrentOnDeactivated;

    }

    private bool isDeactivated = true;

    private void CurrentOnDeactivated(object sender, EventArgs eventArgs)
    {
        isDeactivated = true;
        //handle case where another app gets focus
    }

    private void CurrentOnActivated(object sender, EventArgs eventArgs)
    {
        if (isDeactivated)
        {
            //Ok, this app was in the background but now is in the foreground,
            isDeactivated = false;

            //TODO: bring windows to forefont                
            MessageBox.Show("activated");

        }
    }
}

Upvotes: 1

TheGeneral
TheGeneral

Reputation: 81573

I think what you are experiencing is the window Owner not being set on your child window.

Please refer to Window.Owner Property

When a child window is opened by a parent window by calling ShowDialog, an implicit relationship is established between both parent and child window. This relationship enforces certain behaviors, including with respect to minimizing, maximizing, and restoring.

-

When a child window is created by a parent window by calling Show, however, the child window does not have a relationship with the parent window. This means that:

  • The child window does not have a reference to the parent window.

  • The behavior of the child window is not dependent on the behavior of the parent window; either window can cover the other, or be minimized, maximized, and restored independently of the other.

You can easily fix this by setting the Owner property in the child window when before calling Show() or ShowDialog()

Window ownedWindow = new Window();
ownedWindow.Owner = this; // this being the main or parent window
ownedWindow.Show();

Note : if conforming to an MVVM pattern, this can become a little more cumbersome, however there is plenty of resources on how to link owner and parent windows.

Upvotes: 1

Related Questions