WPF intercept clicks outside a modal window

后端 未结 2 614
日久生厌
日久生厌 2021-01-21 07:38

Is it possible to check when the user has clicked outside a modal window? I\'d like to somehow circumvent the modal logic because if the window isn\'t displayed as modal, it wil

2条回答
  •  一生所求
    2021-01-21 08:42

    Well one way is to hook up the event handler on your main app and respond to it when you have that window open:

    EventManager.RegisterClassHandler(typeof(Window), Mouse.MouseDownEvent, new MouseButtonEventHandler(OnMousepDown), true);
    

    or

      EventManager.RegisterClassHandler(typeof(yourAppClassName),   Mouse.PreviewMouseDownEvent, new MouseButtonEventHandler(OnMousepDown), true);
    
    //this is just a sample..
    private void OnMousepDown(object sender, MouseButtonEventArgs e)
        {
            if (thatWindowThatYourTalkingAbout.IsOpen) 
                ..do something 
        }
    

提交回复
热议问题