Search code examples
wpfeventsmodal-dialogwindow

Application.Current.Activated doesn't fire while .ShowDialog() active


I am using Application.Current.Activated to bring all windows to the front when any one is clicked. This is working great, but doesn't work when I have a modal dialog open using .ShowDialog()

Clicks on other windows of the application (besides the dialog) do not result in the application being activated. The default behavior is to do nothing. (The clicks appear to be entirely IGNORED!)

  • How can I make it so that when any window of the application is clicked, the open dialog is brought to the front? (The expected behavior based on how pretty much every application works)

YES: I did set the owner of the dialog to my MainWindow, and clicking on the main window produces the desired result, but clicking any other window does nothing.

Code:
Create a new WPF project and add two windows.
Then put a button on MainWindow.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Window1 foo = new Window1();
        foo.Show();
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Window2 modal = new Window2();
        modal.Owner = this;
        modal.ShowDialog();
    }
}

To see what I'm talking about, click the button to open the modal dialog.
The desired behavior is what happens when MainWindow is clicked.
I want the same to happen when Window1 is clicked.


Solution

  • AFAIK, a modal dialog (which .ShowDialog() triggers) blocks all other windows thereby Activation events.

    As per the MSDN:

    When a Window class is instantiated, it is not visible by default. ShowDialog shows the window, disables all other windows in the application, and returns only when the window is closed. This type of window is known as a modal window.

    http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog(v=vs.110).aspx

    This may be an ugly hack, but maybe you can accomplish what you need to by creating your modal window on a separate thread thereby letting your main application continue to process activation (and other) events? You would need a way to prevent 2 of that 2nd modal dialog from opening though.