147

I followed this small "tutorial" on how to add a scrollbar to an ItemsControl, and it works in Designer view, but not when I compile and execute the program (only the first few items show up, and no scrollbar to view more - even when VerticalScrollbarVisibility is set to "Visible" instead of "Auto").

Any idea on how to solve this?


This is the code I use to show my items (normally I work with Databinding, but to see the items in my Designer I added them manually):

<ItemsControl x:Name="itemCtrl" Style="{DynamicResource UsersControlStyle}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Top">
            </StackPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
</ItemsControl>

And this is my Template:

<Style x:Key="UsersControlStyle" TargetType="{x:Type ItemsControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ItemsControl}">
                <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                    <ScrollViewer VerticalScrollBarVisibility="Visible">
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

3 Answers 3

301

To get a scrollbar for an ItemsControl, you can host it in a ScrollViewer like this:

<ScrollViewer VerticalScrollBarVisibility="Auto">
  <ItemsControl>
    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
    <uc:UcSpeler />
  </ItemsControl>
</ScrollViewer>
8
  • 18
    It's so obvious when you see it... As I'm coming from Windows Forms, I often find myself stuck in the wrong mindset. It seems that WPF rights a lot of wrongs... +1. Nov 9, 2010 at 8:26
  • 4
    Thanks -very helpful. I agree with Lette that my WinForms brain doesn't "get" this initially.
    – itsmatt
    Dec 14, 2010 at 20:22
  • 2
    I just tried this right here and it still did not work. The ItemsControl flows right off its parent container and no ScrollBar is visible at all.
    – Ristogod
    Jun 21, 2011 at 16:36
  • 11
    @Ristogod If you host the ScrollViewer inside something that lets its content grow as much as needed, for example a StackPanel, scrolling will not work. What's the parent container? Try setting a fixed height on either the ScrollViewer or the parent, does that help?
    – Oskar
    Jun 22, 2011 at 6:56
  • 5
    @Rod You can host the ScrollViewer in a DockPanel or a Grid instead of a StackPanel to achieve this.
    – Oskar
    Oct 16, 2011 at 14:38
96

You have to modify the control template instead of ItemsPanelTemplate:

<ItemsControl >
    <ItemsControl.Template>
        <ControlTemplate>
            <ScrollViewer x:Name="ScrollViewer" Padding="{TemplateBinding Padding}">
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </ItemsControl.Template>
</ItemsControl>

Maybe, your code does not working because StackPanel has own scrolling functionality. Try to use StackPanel.CanVerticallyScroll property.

3
  • 1
    The StackPanel CanVerticallyScroll property didn't work, I'm afraid.
    – Xuntar
    Jan 8, 2010 at 16:29
  • StackPanel CanVerticallyScroll didnt work but the code example given here worked for me. Thanks May 31, 2011 at 18:43
  • This works. I am looking for making scrollviewer inside instead of outside, because github.com/punker76/gong-wpf-dragdrop requires it.
    – HelloSam
    Jul 25, 2013 at 5:32
3

Put your ScrollViewer in a DockPanel and set the DockPanel MaxHeight property

[...]
<DockPanel MaxHeight="700">
  <ScrollViewer VerticalScrollBarVisibility="Auto">
   <ItemsControl ItemSource ="{Binding ...}">
     [...]
   </ItemsControl>
  </ScrollViewer>
</DockPanel>
[...]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.