Skip to main content
Microsoft Silverlight
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Listbox scroll problem with databinding
6 replies. Latest Post by Mog Liang - MSFT on October 14, 2009.
(0)
aaadrian
Member
2 points
8 Posts
10-08-2009 1:17 PM |
Hi,
I'm populating a listbox using data binding. When I change the underlying data, the contents of the listbox change, as you'd expect. However, the vertical scroll position of the listbox does not change. This can cause problems. For example, if I switch from a long list of data to a short list, the scroll position can become completely invalid.
I've tried calling ScrollIntoView(newFirstItem) after changing the data. This indeed scrolls the listbox to the first item, but it does not change the position of the vertical scrollbar, so the listbox contents are now misaligned with the scrollbar. I don't seem to have any other direct access to the scrolling.
Is there any way around this? Is it a bug?
Thanks very much
Adrian
lee_sl
Contributor
3140 points
614 Posts
10-08-2009 2:10 PM |
You can get the scrollviewer and modify the offset. this post might help
http://leeontech.wordpress.com/2009/10/08/listbox-and-scrolling/
10-09-2009 12:23 AM |
Thanks for the response. But actually, this doesn't solve the problem. It works if the vertical scrollbar is active both before and after the source data is changed, ie if both lists are too long for the available space. But if one list fits into the listbox with no scrolling and the other does not, then this approach fails - the list itself is scrolled correctly to the starting point, but the scrollbar is not, so they become misaligned.
To see this problem in action, you can slightly modify the project you linked to above so it has 2 buttons, which set the list lengths to 6 and 20 respectively. Click on the 20. Scroll down. Click on the 6. Scrollbars have gone. Click back on the 20. Everything's messed up.
So apparently you can't reliably use databinding to populate a listbox? Am I missing something obvious?
10-09-2009 10:28 AM |
You are right. looks like there are issues with the positioing of the scrollbar
Mog Lian...
All-Star
16551 points
1,595 Posts
10-12-2009 11:10 PM |
This is an known issue caused by ListBox virtualization. Currently, an workaround is replace VirtualizingStackPanel with StackPanel, however, this will drop the listbox performance.
<ListBox MaxHeight="100" Name="lb1"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel/> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox>
Thanks,
10-14-2009 12:06 AM |
Thanks very much, that does fix it. (However, it does NOT appear to work with the method in the project linked above that uses GetChild and ScrollToVerticalOffset. Instead you have to use ScrollIntoView.)
Too bad this solution degrades performance. Would this be better handled procedurally in code rather than via databinding? Is databinding considered ready for prime time yet?
10-14-2009 1:25 AM |
aaadrian:Too bad this solution degrades performance. Would this be better handled procedurally in code rather than via databinding? Is databinding considered ready for prime time yet?
VirtualizatingStackPanel used optimization technology that it only create and initalize elements which is visible. If replace it with stackpanel, the listbox will create all items once set itemssource. Unless you implement similar function as virtualzatingStackPanel, procedurally and databinding would have same performance.