Skip to main content

Answered Question Listbox scroll problem with databindingRSS Feed

(0)

aaadrian
aaadrian

Member

Member

2 points

8 Posts

Listbox scroll problem with databinding

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
lee_sl

Contributor

Contributor

3140 points

614 Posts

Re: Listbox scroll problem with databinding

You can get the scrollviewer and modify the offset. this post might help

http://leeontech.wordpress.com/2009/10/08/listbox-and-scrolling/

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

aaadrian
aaadrian

Member

Member

2 points

8 Posts

Re: Re: Listbox scroll problem with databinding

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?

lee_sl
lee_sl

Contributor

Contributor

3140 points

614 Posts

Re: Re: Re: Listbox scroll problem with databinding

You are right. looks like there are issues with the positioing of the scrollbar

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

Mog Liang - MSFT
Mog Lian...

All-Star

All-Star

16551 points

1,595 Posts

Answered Question

Re: Re: Re: Listbox scroll problem with databinding

Hi,

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,

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

aaadrian
aaadrian

Member

Member

2 points

8 Posts

Re: Re: Re: Listbox scroll problem with databinding

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?

Mog Liang - MSFT
Mog Lian...

All-Star

All-Star

16551 points

1,595 Posts

Re: Re: Re: Listbox scroll problem with databinding

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.

Thanks,

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement