Item realization issues with ScrollToHorizontalOffset (virtualization)

Thread is closed for posting
4 posts, 1 answers
  1. Rajen
    Rajen avatar
    4 posts
    Member since:
    Apr 2012

    Posted 20 Oct 2013 Link to this post


    I'm using the RadDataBoundListBox control to display a large number of images (binding to IEnumerable<byte[]> and using a Converter to display the actual images). Everything seems to be working properly, but I have a requirement to manually scroll through the list using pixel offsets.

    I can get the list to scroll with the following code:

    private ScrollViewer _listBoxScrollViewer;
    _listBoxScrollViewer = listBox.ChildrenOfType<ScrollViewer>().FirstOrDefault();
    _listBoxScrollViewer.ScrollToHorizontalOffset(_listBoxScrollViewer.HorizontalOffset + 512);

    The problem occurs after scrolling a few times to the right where the items haven't realized yet (due to the virtualization). The problem is that calling ScrollToHorizontalOffset does not actually trigger the realization of the items, so the list becomes blank after scrolling using this method. The only way to trigger the items to realize is to manually touch the RadDataBoundListBox control, which pretty much defeats the purpose.

    Is there any way to trigger the item realization when manually scrolling a RadDataBoundListBox with ScrollToHorizontalOffset?

  2. Answer
    Deyan avatar
    2147 posts

    Posted 21 Oct 2013 Link to this post

    Hello Rajen,

    Thanks for writing.

    RadDataBoundListBox implements a custom UI virtualization mechanism that uses the change of visual states of the ScrollViewer (scrolling -> not-scrolling -> scrolling) to trigger the UI virtualization logic. When you directly set the scrolloffset of the ScrollViewer component the UI virtualization mechanism is bypassed and therefore you get the undesired behavior you observe.

    While in some cases explicitly setting a scroll offset might work as expected, in other cases this might not yield the expected result. Imagine that you set a big enough scroll offset that makes all realized items be recycled and disappear: what would be the data index to start realizing from based on the scroll offset provided that items may have different sizes? We may use some predictions but ultimately the behavior will not be deterministic.

    Otherwise you can trigger the virtualization logic by calling the BalanceVisualSpace method which is defined by the base RadVirtualizingDataControl class and is internal. You should use reflection to do that.

    I hope this helps.

    Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
  3. Rajen
    Rajen avatar
    4 posts
    Member since:
    Apr 2012

    Posted 21 Oct 2013 Link to this post

    Hi Deyan,

    Thanks for the elaboration, your suggestion to manually call the BalanceVisualSpace method worked! For future reference, here's how to call it:

    private MethodInfo _balanceVisualSpaceMethod;
    _balanceVisualSpaceMethod = listBox.GetType().GetMethod("BalanceVisualSpace", BindingFlags.NonPublic | BindingFlags.Instance);
    _balanceVisualSpaceMethod.Invoke(listBox, null);

  4. Alex
    Alex avatar
    25 posts
    Member since:
    Mar 2011

    Posted 06 Feb 2014 Link to this post

    Thank you for all your posts and advices.
    I decided not to use reflection, but still call BalanceVisualSpace.
    After 5 minutes in JustDecompile, I ended with following code:

    01.//I checked exact value of RealizedItemsBufferScale on exact page and it's 2 by default in my case
    02.var currentBufferScale = telerikListBox.RealizedItemsBufferScale;
    03.if (currentBufferScale >  2.0000002)
    05.    currentBufferScale -= 0.0000001;
    09.    currentBufferScale += 0.0000001;
    11.telerikListBox.RealizedItemsBufferScale = currentBufferScale;

    Telerik Team, please make method BalanceVisualSpace public in upcoming release.
    Thank you.

Back to Top