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 ?

Marc
  • 16,170
  • 20
  • 76
  • 119
droopy6
  • 191
  • 1
  • 6

5 Answers5

10

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.

droopy6
  • 191
  • 1
  • 6
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
            });
        }
    }
}
Lyra
  • 465
  • 1
  • 4
  • 13
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);
}
yumaikas
  • 979
  • 13
  • 24
XamlZealot
  • 693
  • 5
  • 12
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);
}
XamlZealot
  • 693
  • 5
  • 12
  • 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. – droopy6 Nov 08 '12 at 10:20
  • damned, this is working outside of my project, with my styles ! I don't understand why this is not working... – droopy6 Nov 08 '12 at 10:36
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?

bonCodigo
  • 14,268
  • 1
  • 48
  • 91