Scrollable Region and IScroll Info

If you place a panel or any other ordinary element inside a ScrollViewer, the ScrollViewer will measure its size in the normal way: the scrollable area essentially sizes to content (unless the available area is surplus to requirements, in which case the ScrollViewer gives the child all of the available space). It keeps track of the currently visible region, and moves the child content around as required. Most of the time, this is exactly the behavior you require. However, occasionally you might need to take a bit more control.

For example, if you have a large scrollable area containing lots of items, it might not be very efficient to create all of the items upfront. You might be able to improve performance significantly by creating items on demand only as they scroll into view. Such tricks require you to get more deeply involved in the scrolling process.

If you want to take control of how scrolling functions, you must write a user interface element that implements IScrollInfo. ScrollViewer looks for this interface on its child element. If the child implements the interface, the ScrollViewer will no longer pretend that the child has all the space it requires—instead, it will tell the child exactly how much space is available on-screen for the viewport, and will defer to the child for all scrolling operations. In this case, the ScrollViewer's role is reduced to showing scroll bars and notifying the child when the user attempts to scroll.

This is not a step to be taken lightly. IScrollInfo has 24 members, and requires you to do most of the work that ScrollViewer would otherwise have done for you.* Fortunately, for the very common scenario of scrolling through a list, we can use the built-in IScrollInfo implementation provided by VirtualizingStackPanel. The VirtualizingStackPanel implements IScrollInfo so that it can show scroll feedback for all of the data, even though it only generates UI elements to represent those items currently visible, "virtualizing" the view of the data. You don't need to take any special steps to enable virtualization—a data-bound ListBox automatically displays its items using a VirtualizingStackPanel. You would need to implement IScrollInfo only if you are not using data binding, or if you need something other than a simple linear stack of items.

If you customize the appearance of an ItemsControl using the template techniques described in Chapters 8 and 9, you might end up disabling virtualization. To avoid this, you should ensure that if you change the Template or ItemsPanelTemplate property of an ItemsControl, your replacement template contains a VirtualizingStackPanel.

* For a full example of how to implement IScrollInfo, see a series of three articles on this subject, written by a Microsoft developer, at (

We have now looked at all of the built-in mechanisms for helping you manage your application's layout. But what if you have unusual requirements that are not met by the built-in panels? Sometimes it is necessary to customize the layout process by writing your own panel.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment