AutoCompleteComboBox Arrow Up/Arrow Down keys to s

2019-02-24 08:24发布

问题:

I created a simple AutoCompleteBox in my WPF app and it loads great with code intercepting the Populate event, but when the list pops up and I hit the arrow down key and get to the end of the list the vertical scroll bar doesn't scroll.

The values keep changing in the field like it is scrolling through them, but the scroll bar doesn't move.

If I use the mouse it scrolls fine.

I just need the arrow key to scroll it.

Any ideas/suggestions?

I am new to WPF and have searched forever for this fix.

回答1:

I see the same behavior. I found a post on codeplex talking about a different issue but at the bottom of the post they have a class AutoCompleteBoxEx that supports ScrollIntoView, so you can hook up the SelectionChanged even and this should get you the behavior you want. I have no idea why this is not baked in. I have had a chance to test out the posted code.

Update

Just pasted the code from the post into a class and used it in the XAML by changing AutoCompleteBox to AutoCompleteBoxEx and adding namespace for AutoCompleteBoxEx and it worked fine. You don't have to specify any event in the XAML, nor do you need to add any code to the code behind.



回答2:

Attach a SelectionChanged event and then, inside the handler:

private void AutoCompleteBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    AutoCompleteBox box = (AutoCompleteBox)sender;
    ListBox innerListBox = (ListBox) box.Template.FindName("Selector", box);
    innerListBox.ScrollIntoView(innerListBox.SelectedItem);
}