WPF Popup hiding problem

后端 未结 6 1608
耶瑟儿~
耶瑟儿~ 2021-01-13 22:59

Suppose you have a ToggleButton for opening a Popup, same behaviour as all known elements as ComboBox etc.

... which is this c

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 23:21

    I found the solution on this post : https://stackoverflow.com/a/5821819/651161

    Using the following class will permit to handle the click before the togglebutton is pressed. The popup is closed because of the click but the click is then handled, so it doesn't trigger the ToggleButton click.

    public class MyPopup : Popup {
        protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e) {
            bool isOpen = this.IsOpen;
            base.OnPreviewMouseLeftButtonDown(e);
    
            if (isOpen && !this.IsOpen)
                e.Handled = true;
        }
    }
    

提交回复
热议问题