AutoCompleteBox ScrollIntoView Issue

Apr 20, 2010 at 10:46 PM

I'm using an AutoCompleteBox bound to a list of customers. When there are many customers in the populated list, there is a vertical scroll bar present in the popup. However, when using the the up/down keys to select a customer from the list, the selected item doesn't "scroll into view" in the ListBox (the scroll bar doesn't move so that the selected customer is in view). Is there a way to fix this?

May 26, 2010 at 5:53 PM
Same problem here. This is the workaround I use. I'm using templating and I handle the event SelectionChanged on the listbox. In the method associated to this event, I do : private void Selector_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBox box = ((ListBox)sender); box.ScrollIntoView(box.SelectedItem); e.Handled = true; } If you need help with template, see AutoCompleteComboBox.xaml in the samples.
Jan 5, 2012 at 1:05 AM

Based on a codeplex response I created a simple class to extend AutoCompleteBox to fix this. Write once and forget about it, except you use AutoCompleteBoxEx instead of AutoCompleteBox and add an xlmns to whatever namespace you put this class into.

   public class AutoCompleteBoxEx : AutoCompleteBox
    {
        private bool _selectorIsListBox = false;
        private bool _selectorSelectionChangedHandlerRegisterd = false;
        private const int SELECTOR_MAX_HEIGHT = 250;
        private TextBox _textBox;
        private ListBox _selector;

        public AutoCompleteBoxEx()
        {
            Loaded += AutoCompleteBoxExLoaded;
        }

     

        protected void AutoCompleteBoxExLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (_selector == null)
            {
                _selector = Template.FindName("Selector", this) as ListBox;

                if (_selector != null)
                    _selectorIsListBox = true;
            }

            if (!_selectorSelectionChangedHandlerRegisterd && _selectorIsListBox && _selector != null)
            {
                _selector.SelectionChanged += ListBoxSelectionChanged;
                _selector.MaxHeight = SELECTOR_MAX_HEIGHT;
                _selectorSelectionChangedHandlerRegisterd = true;
            }
        }



        static void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox box = ((ListBox)sender);
            box.ScrollIntoView(box.SelectedItem);
            e.Handled = true;
        }
    }