0

I have a ListBox which displays its items in a WrapPanel. The ListBox itself is embeddded in Border. But the more items I put into the ListBox, the larger window gets. How can I prevent that and display the vertical scrollbar instead?

I found several other posts but nothing worked so far.

ListBox

            <Border Background="WhiteSmoke" BorderBrush="Gray" BorderThickness="4" Margin="5"> 
            <ListBox Background="Transparent" BorderThickness="0" Height="Auto" 
                     ItemsSource="{Binding Path=Snapshots, RelativeSource={RelativeSource AncestorType=Demo:CameraCanvas}}"
                     SelectedItem="{Binding Path=Snapshots.SelectedSnapshot, RelativeSource={RelativeSource AncestorType=Demo:CameraCanvas}}"
                     ScrollViewer.VerticalScrollBarVisibility="Auto"
                     ScrollViewer.CanContentScroll="True"
                     >
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel 
                            Width="{Binding (FrameworkElement.ActualWidth), 
                            RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" 
                            ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
                            MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}" 
                            ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border CornerRadius="5" Background="WhiteSmoke" BorderBrush="Gray" BorderThickness="2" Margin="5" Padding="2">
                            <StackPanel Orientation="Vertical">
                                <Image Source="{Binding Path=Image, UpdateSourceTrigger=PropertyChanged}" Width="64" Height="64" Stretch="Uniform" StretchDirection="Both" />
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            </Border>
Matthias
  • 5,574
  • 8
  • 61
  • 121

1 Answers1

1

Put the ListBox in a DockPanel, the dockpanel should be constrainted in size.

read the following msdn topic Panels Overview

makc
  • 2,569
  • 1
  • 18
  • 28
  • But the DockPanel (ListBox) is shown on the right of my window from top to bottom. When I resize the window, the listbox should of course grow in size. But you are saying, I should set a size constraint? – Matthias Nov 20 '12 at 15:18
  • @Matthias Try setting a size and you'll see that the scrollviewer will appear. The dock panel is constrainted on both X and Y dimentions so it will receive its size from its visual parent and wont allow the listbox to grow over this size. – makc Nov 20 '12 at 15:27
  • Didn't work. My first control is a Grid and on the right cell I do have the Border with the inner ListBox/WrapPanel. If I put a DockPanel around the Border saying LastChildFill=true it still does not work. I will try it in another prototype and let you know. – Matthias Nov 20 '12 at 15:35
  • @Matthias set the grid cells row size to be "*" and not "auto" – makc Nov 20 '12 at 15:37
  • 1
    Thanks, it worked now. One of my major problems was, that the main window had a property "SizeToContent" set. – Matthias Nov 21 '12 at 06:54