Prevent scrolling when mouse enters WPF ComboBox dropdown
One way to solve this is to use a Behaviour (or rather behaviour-like Attached Property) to subscribe to the RequestBringIntoView
event of the ComboBoxItems
and then set the RequestBringIntoViewEventArgs.Handled
to true.
This can also be done on a small scale using an EventSetter
and codebehind.
<Style TargetType="ComboBoxItem">
<EventSetter Event="RequestBringIntoView" Handler="OnRequestBringIntoView"/>
</Style>
private void OnRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
//Allows the keyboard to bring the items into view as expected:
if (Keyboard.IsKeyDown(Key.Down) || Keyboard.IsKeyDown(Key.Up))
return;
e.Handled = true;
}
Edit
I found that you can get the same effect by handling the RequestBringIntoView event on the ItemsPanel
rather than the items themselves. But same result:
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel RequestBringIntoView="OnRequestBringIntoView"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
From what I can tell this seems to be caused by the items at the bottom of the light being "partially displayed", where an item is truncated by the container. When the mouse goes over a partial item like this WPF scrolls the entire item in to view, which can sometimes leave another partial item at the bottom.
In Winforms this can be fixed by setting the .IntegralHeight
, but no such property exists in WPF from what I can tell. If all the items in your combobox have the same height you could bind the height of the list of the combobox to a multiple of the item height, for example, display 10 x 20px tall items, set it to 200.