Disable MouseWheel in editable ComboBox as ItemTemplate

I am using ComboBox as an ItemTemplate inside a ListBox. My ComboBox is editable. When the user uses the mouse wheel in the combo box, it changes the current value. I do not want it. I want the ListBox to scroll. Are there any solutions? Most of the examples I found are based on the ComboBox just read. It seems that none of the solutions I found work. overriding the OnMouseWheel parameter isHandled = true does not work, it looks like this event is being processed elsewhere. I tried to override OnMouseWheel in a TextBox used by the ControlTemplate of my ComboBox without success.

any ideas?

+6
source share
5 answers

Ok, my mistake, I set the PreviewMouseWheel to the wrong UIElement of my ItemTemplate. So this works:

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

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

+5
source

I solved a similar problem once with the following approach:

WPF:

 <ComboBox MouseWheel="ComboBox_MouseWheel"/> 

FROM#:

 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); } 
+2
source

Try registering the 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); } 
+1
source

I solved your problem with 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 }); } } } 
+1
source

Is it right to say that our case is similar to the "Font List" field on the toolbar: selecting a new font where the previously selected font is still displayed as the selected value, but can you scroll it vertically?

In this case, can you consider the sample as such? creating a Font Box window .

Further link: Could you check out this MSDN article?

0
source

All Articles