Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

AutoCompleteBox: How to scroll selected item into view? RSS

3 replies

Last post Feb 12, 2010 08:48 AM by kobruleht

(0)
  • mehroz

    mehroz

    Member

    658 Points

    129 Posts

    AutoCompleteBox: How to scroll selected item into view?

    Feb 09, 2010 12:23 PM | LINK

    Hi all,

    Is there anyway to scroll any particular item into view. I hooked the OnPopulated event and called the underlying ListBox's ScrollIntoView() but it has no effect. I also tried UpdateLayout() before/after but no avail. Any ideas?

    Regards,
  • nangua

    nangua

    Contributor

    5288 Points

    862 Posts

    Re: AutoCompleteBox: How to scroll selected item into view?

    Feb 11, 2010 08:25 AM | LINK

    ok, I searched a lot and finally found that you need to put ListBox.ScrollIntoView in Dispatcher.BeginInvoke:

    I add a Loaded event to the ListBox which is in the template of AutoCompleteBox:

    private void Selector_Loaded(object sender, RoutedEventArgs e)
    {
        ListBox listBox = sender as ListBox;
        Dispatcher.BeginInvoke(
            () =>
            {
                listBox.ScrollIntoView(listBox.Items[17]);
            }
        );  
    }

    Have a try and let me know if it works for you

  • mehroz

    mehroz

    Member

    658 Points

    129 Posts

    Re: AutoCompleteBox: How to scroll selected item into view?

    Feb 11, 2010 01:02 PM | LINK

     Wow!!!...That worked perfectly. Thanks very much for your time and effort. I shall very soon be updating my recent article: http://www.codeproject.com/KB/silverlight/AutoComplete_ComboBox.aspx with this fix and a mention of your name.

     Have a great day!

    Regards,

  • kobruleht

    kobruleht

    Member

    384 Points

    1083 Posts

    Re: AutoCompleteBox: How to scroll selected item into view?

    Feb 12, 2010 08:48 AM | LINK

     This does not work if  box is opened first time. It works in box is opened in second time. If opened for third and more times it still does not work. how to fix ?

     

            protected override void OnPopulated(PopulatedEventArgs e)
            {   base.OnPopulated(e);
                ListBox listBox = GetTemplateChild("Selector") as ListBox;
                if (listBox != null)
                {
                    if (SelectedItem == null && ItemsSource != null)
                    {
                        foreach (PickListEntity item in ItemsSource)
                            if (item.DisplayMember == Text)
                            {
                                SelectedItem = item;
                                break;
                            }
                    }
    
                    //highlight the selected item, if any
                    if (SelectedItem != null)
                    {
                        listBox.SelectedItem = SelectedItem;
                        // todo: why this does not scroll into view on first open:
                        Dispatcher.BeginInvoke(() =>
                        {
                            listBox.SelectedItem = SelectedItem;
                            Dispatcher.BeginInvoke(() =>
                            {
                                listBox.ScrollIntoView(SelectedItem);
                            });
                        });
                    }
                }
            }
     

    Microsoft Community Contributor