Reputation: 301
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 ?
Upvotes: 9
Views: 4836
Reputation: 465
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
});
}
}
}
Upvotes: 4
Reputation: 722
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);
}
Upvotes: 1
Reputation: 301
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.
Upvotes: 11
Reputation: 722
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);
}
Upvotes: 1
Reputation: 14361
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?
Upvotes: 0