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 ?
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.
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
});
}
}
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With