How to get the click outside window event

78 Views Asked by At

I make a very simple window:

var wndPpList = new Window()
{
    SizeToContent = SizeToContent.WidthAndHeight,
    WindowStyle = WindowStyle.None,
    ShowInTaskbar = false,
    BorderBrush = GetColour_Cyan(),
    BorderThickness = new Thickness(3),
};

now I want to make that when I click outside it, it closes.

I have seen a lot of posts where in order to achieve this, they activate the mouse

wndPpList.CaptureMouse();
wndPpList.Deactivated += (sender, args) => 
{
    wndPpList.ReleaseMouseCapture();
    wndPpList.Close();
};

but the event Deactivated is just not fired.

Thanks for any help

Patrick

1

There are 1 best solutions below

5
jeb On BEST ANSWER

Using the MouseDown event you can check if the event was raised outside of the popup. Then you can close the popup window.

        public MainWindow()
        {
            InitializeComponent();

            var wndPpList = new Window()
            {
                SizeToContent = SizeToContent.WidthAndHeight,
                WindowStyle = WindowStyle.None,
                ShowInTaskbar = false,
                BorderBrush =  Brushes.Cyan,
                BorderThickness = new Thickness(3),
            };
            wndPpList.Show();

            // Handle the mouse down event of the application window
            Application.Current.MainWindow.MouseDown += (s, e) =>
            {
                if (!wndPpList.IsMouseOver)
                {
                    wndPpList.Close();
                }
            };
        }

if you need to fire the event more then one time you need to register and unregister it properly. This is an example with a button to open the popup. However if you open two popup windows at the same time you will get propblems. So you should disable the button while the popup is shown or use some other machanism to only show one popup.

public partial class MainWindow : Window
    {
        private MouseButtonEventHandler windowMouseDownHandler;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            UnregisterMouseDownEvent();
            var wndPpList = new Window()
            {
                SizeToContent = SizeToContent.WidthAndHeight,
                WindowStyle = WindowStyle.None,
                ShowInTaskbar = false,
                BorderBrush = Brushes.Cyan,
                BorderThickness = new Thickness(3),
            };
            wndPpList.Show();

            windowMouseDownHandler = (s, e) =>
            {
                if (!wndPpList.IsMouseOver)
                {
                    wndPpList.Close();
                }
            };
            // Register the event handler function
            Application.Current.MainWindow.MouseDown += windowMouseDownHandler;
        }

        private void UnregisterMouseDownEvent()
        {
           
            if (windowMouseDownHandler != null)
            {
                Application.Current.MainWindow.MouseDown -= windowMouseDownHandler;
            }
           
        }