Handling MouseWheel event in a WPF custom control

4

I have am making a map editor for a game which has a user control that has an image. Inside that control I attached the MouseWheel event to it, but I've noticed two issues that I hope to have a better understanding of why it behaves the way it does and how to properly implement it.

For one the event only seems to fire when the mouse is hovering over it instead of when the control is in focus. If possible I would like to switch that and be able to fire the event no matter where the mouse is as long as that control is in focus and the second issue is that checking the delta when the number is positive works fine, but when I get a number back when it's negative I get a value of 0xfffffffd or something in that range. How would I go about differentiating the difference between a positive balue and a negative value if I always get something positive?

Thanks in advance for the help.

wpf
mousewheel
asked on Stack Overflow Jan 15, 2013 by Seb

1 Answer

-1

If you want to fire MouseWheel event for focused element try:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.MouseWheel += OnMouseWheel;
    }

    IInputElement focusedElement;

    private void OnMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (focusedElement is TextBox)
        {
            var tbx = focusedElement as TextBox;

            //do something
        }
    }

    protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
    {
        focusedElement = e.NewFocus;
    }
}
answered on Stack Overflow Jan 15, 2013 by Rafal

User contributions licensed under CC BY-SA 3.0