Disable MouseWheel in editable ComboBox as ItemTemplate
Asked Answered
H

5

9

I use a ComboBox as ItemTemplate inside a ListBox. My ComboBox is editable. When the user use the mouse wheel in the combobox, it change the current value. I don't want that. I want the ListBox to scroll. Is there any solution to this ? Most examples I found are based only on a readonly ComboBox. It seems that none of the solution I found works. override OnMouseWheel setting isHandled = true does not work it seems the event is handled in other places. I tried to override OnMouseWheel in the TextBox used by the ControlTemplate of my ComboBox without success.

any ideas ?

Harrus answered 7/11, 2012 at 14:29 Comment(0)
H
11

Okay, my mistake, I put PreviewMouseWheel on a wrong UIElement of my ItemTemplate. So this is working:

private void myCombo_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    e.Handled = true;
}

Nevertheless, the "parentListBox.RaiseEvent(args);" does not work.

Harrus answered 8/11, 2012 at 10:45 Comment(0)
I
4

I solved your problem with a Behavior (and the logic provided by @XamlZealot):

internal class ComboBoxIsNotScrollingItemsBehavior : Behavior<ComboBox>
{
    protected override void OnAttached()
    {
        this.AssociatedObject.PreviewMouseWheel += this.ComboBox_PreviewMouseWheel;
    }

    protected override void OnDetaching()
    {
        this.AssociatedObject.PreviewMouseWheel -= this.ComboBox_PreviewMouseWheel;
    }

    private void ComboBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (this.AssociatedObject.IsDropDownOpen == false)
        {
            e.Handled = true;

            ((FrameworkElement)this.AssociatedObject.Parent).RaiseEvent(new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
            {
                RoutedEvent = UIElement.MouseWheelEvent,
                Source = sender
            });
        }
    }
}
Impasse answered 6/5, 2016 at 18:36 Comment(0)
G
1

I solved a similar issue once with the following approach:

WPF:

<ComboBox MouseWheel="ComboBox_MouseWheel"/>

C#:

private void ComboBox_MouseWheel(object sender, MouseWheelEventArgs e)
{
    e.Handled = true;
    MouseWheelEventArgs args = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
    args.RoutedEvent = UIElement.MouseWheelEvent;
    args.Source = sender;
    parentListBox.RaiseEvent(args);
}
Goodtempered answered 7/11, 2012 at 15:19 Comment(1)
this does not work with editable combobox. I don't even get into the handler.Harrus
G
1

Try registering a class handler in your constructor:

EventManager.RegisterClassHandler(typeof(ComboBox), ComboBox.MouseWheelEvent, new RoutedEventHandler(MouseWheeled));

private void MouseWheeled(object Sender, RoutedEventArgs e)
{
    MouseWheelEventArgs mouseArgs = (MouseWheelEventArgs)e;
    e.Handled = true;
    MouseWheelEventArgs args = new MouseWheelEventArgs(mouseArgs.MouseDevice, mouseArgs.Timestamp, mouseArgs.Delta);
    args.RoutedEvent = UIElement.MouseWheelEvent;
    args.Source = Sender;
    parentListBox.RaiseEvent(args);
}
Goodtempered answered 7/11, 2012 at 16:3 Comment(2)
PreviewMouseWheel do the trick but this is not working with my combobox styles. If I test on a simple project, the mouse wheel is properly disabled.Harrus
damned, this is working outside of my project, with my styles ! I don't understand why this is not working...Harrus
O
0

Is it correct to say our case is like, Font list box in toolbar: choosing a new font where previously selected font is still appear as the selected value, yet you could scroll vertically?

In that case can you consider a sample like this? creating a Font Box as well.

Further reference: Could you check on this MSDN article?

Oscillograph answered 7/11, 2012 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.