Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable MouseWheel in editable ComboBox as ItemTemplate

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 ?

like image 563
droopy6 Avatar asked Nov 07 '12 14:11

droopy6


3 Answers

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.

like image 143
droopy6 Avatar answered Nov 12 '22 18:11

droopy6


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
            });
        }
    }
}
like image 22
Lyra Avatar answered Nov 12 '22 18:11

Lyra


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);
}
like image 2
XamlZealot Avatar answered Nov 12 '22 19:11

XamlZealot