Why does my WrapPanel scroll when it’s used as an ItemsPanel?

I’m customising a ListBox (in WPF, not Silverlight) and I want it to live in a WrapPanel rather than a vertically scrollingStackPanel. So my template looks like this:

        <ItemsPanelTemplate x:Key="DrivePanelTemplate">
                <WrapPanel IsItemsHost="True" Orientation="Vertical" />

Simple enough. And my ListBox definition looks like this:

<ListBox x:Name="Drives" 
                     ItemsPanel="{DynamicResource DrivePanelTemplate}" 
                     ItemTemplate="{DynamicResource DriveTemplate}">

Simple enough. The Height constraint makes sure the items will wrap if there’s enough of them. But when I run it, I get a scroll bar and a single column, not the two columns I want. This seemed odd, since I haven’t specified a scrollviewer.

What I’m missing is that ListBox does specify a scrollviewer and it’s kicking in for me. The fix is to add


to the ListBox declaration, and this stops the implicit scrollviewer kicking in, and the WrapPanel does its job as required. Easy.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s