Thursday, December 3, 2009

How to add scroll viewer with WPF ItemsControl?

How to add scroll viewer with WPF ItemsControl? Here is the trick. The bellow example is for displaying vertical scrollbar only. Some times we have little space and want to have scrollviewer support in ItemsConrol.

Note that to work correctly the ItemsControl need a fixed height. 

<ItemsControl ItemsSource="{Binding}">
<ItemsControl.Template>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled">
<ItemsPresenter></ItemsPresenter>
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl.ItemsPanel>


I guess code explains everything, note that this will work on xbap, WPF, Silverlight.