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?
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 anyif (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);
});
});
}
}
}
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
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:
Have a try and let me know if it works for you
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
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 ?