I
I
iRumba2015-05-08 06:53:26
WPF
iRumba, 2015-05-08 06:53:26

How to send an event to the element with focus?

Hello. There is a TextBox. It has an event handler for the rotation of the mouse wheel. But if the mouse is not over the textbox, then the event does not fire, although the element is under focus. How can I make it catch the mousewheel event when it's under focus, even if the mouse pointer is somewhere else?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bmforce, 2015-05-08
@iRumba

The easiest option is to create a PreviewMouseWheel event near the window (Preview so that the event is called on all child elements) and inside check if the TextBox has focus.

private void TextBox_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            MyFunc();
        }

        private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (tb.IsFocused)
            {
                MyFunc();
            }
        }

The textbox event can be removed altogether if there is no need to trigger an event when the mouse is over it, but there is no focus.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question