Wpf catches a mouse outside my user control

I am creating a custom control in WPF. I want to catch a mouse event when my control is in focus, but the user clicks outside the control. Is there a way to do this, and if so, how?

My control inherits from ListBox.

+3
source share
2 answers

You can use UIElement.CaptureMouse, and its partner UIElement.ReleaseMouseCapture, to capture all mouse events in one control, regardless of what the mouse finished when the event occurred.

In your example, I would capture the mouse when the control has focus, and release the mouse when the control loses focus.

+7

, Window TextBox.

Window MouseDown,

MouseDown += new MouseButtonEventHandler(Window_MouseDown);

private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (TextBox1.IsFocused)
    {
        MessageBox.Show("TextBox1 in focus.");
    }
}

, TextBox1 .

0

Source: https://habr.com/ru/post/1781157/


All Articles