简体   繁体   中英

WPF event on disabled window

Is there any possibility to handle an event on a disabled WPF window? The main window is disabled by .ShowDialog() from other windows. In my application there is only one window enabled at a time and I want to improve the usability. If the user clicks on the wrong (disabled) main window the application should auto focus to the enabled window.

I know that disabled means that the window does not respond to any event, but is there a solution like a global event handler or some special WPF event?

I tried a PreviewMouseLeftButtonDown event but that did not work.

// event called from some special/ global event on disabled window
private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if(App.Current.Windows.Count > 1)
    {
       foreach(Window w in App.Current.Windows)
       {
           if(w.IsEnabled) 
           {
               w.Focus();
               break;
           }
       }          
    }
}

Thanks for your ideas/solutions!

Calling ShowDialog means you want to make window being showed modal , which disables other windows.

Switch this method to Show and you'll be able to use other windows as well.

Refer to this .

Thanks for the hint but as a requirement I've to ensure that the main window is freezed when another window is opened and I've found a solution: Instead of freezing the window with .ShowDialog() I've freezed all controls when a new window is opened with .Show() .

private void DisableAllControls()
{
    // parallel execution cause of many elements
    Parallel.For(0, VisualTreeHelper.GetChildrenCount(this), index =>
    {
        (VisualTreeHelper.GetChild(this, index)
        as UIElement).IsEnabled = false;
    });
}

I also added a MouseDownEvent to get focus an the new window if the user clicks on the "freezed" main window. (There will be only one additional window open at the same time).

private void FocusLastOpen_MouseDown(object sender, MouseEventArgs e)
{
    if (App.Current.Windows.Count > 1)
    {
        foreach (Window w in App.Current.Windows)
        {
            if (w.IsEnabled && w.GetType() != typeof(MainWindow))
            {
                w.Focus();
            }
        }
    }
}

To reactivate the elements of the main window when the other window has closed I've written a static method which will be executed on the ClosingEvent .

public static void EnableAllControls()
{
    MainWindow obj = null;
    foreach(Window w in App.Current.Windows)
    {
        if(w.GetType() == typeof(MainWindow))
        {
            obj = w as MainWindow;
            break;
        }
    }

    if(obj == null) return;
    Parallel.For(0, VisualTreeHelper.GetChildrenCount(obj), index =>
    {
        (VisualTreeHelper.GetChild(obj, index)
        as UIElement).IsEnabled = true;
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM