0
<WrapPanel 
   HorizontalAlignment="Center" 
   ScrollViewer.VerticalScrollBarVisibility="Disabled">
   <GroupBox 
      Header="{DynamicResource ResourceKey=StampUserTab_Strings}" 
      BorderThickness="1.3" 
      Style="{StaticResource GroupBoxBorder}" >
      <ListBox 
         Grid.Row="1" 
         Name="ComponentBox" 
         ItemTemplate="{DynamicResource StringTemplate}" 
         VerticalContentAlignment="Stretch" 
         ScrollViewer.CanContentScroll="True" 
         ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
         ScrollViewer.VerticalScrollBarVisibility="Auto" 
         VerticalAlignment="Stretch" 
         ItemsSource="{Binding ActiveSettings.Components}" 
         SelectionChanged="ComponentBox_SelectionChanged" 
         Style="{StaticResource ResourceKey=SettingsListBoxUser}">
         <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
               <WrapPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
         </ListBox.ItemsPanel>
      </ListBox>
   </GroupBox>
</WrapPanel>

This is my code. The main problem is, that when i add items to the listbox, it keeps expanding out of my screen. I want to make it bound to screen, so when it reaches the bottom, the vertical scroll appers and can be used to scroll the content.

I've tried to set the Height of the grid ro to *, but it didn't help.

Any ideas?

Edit: The problem was the WrapPanel. I changed it to Grid, defined rows and columns with * heights and widths. After adding my elements to the grid rows and colums the listbox were collapsed to scrolls after the expanding. Thank you for the help.

Ádám Kovács
  • 107
  • 2
  • 10
  • So do you want that items should not be added if there is no space for them, and no scroll bar should appear? – Mayur Dhingra Aug 21 '14 at 14:48
  • @ÁdámKovács problem here is the `WrapPanel`. Do you really have only one element in `WrapPanel` or this is simplified code? Maybe you just want to change how items are stacked in `ListBox`? – dkozl Aug 21 '14 at 14:53
  • This might be similar to the problem of putting a `ListBox` into a `StackPanel`. Since the `StackPanel` will grow however large it needs to to fit its children into itself, it will ask the `ListBox` how tall it wants to be and let it be that tall - and therefore the `ListBox` won't have a scrollbar and will be as tall as the contents are combined. I'm not sure about a `WrapPanel`, but try taking it out of there and see what happens. – Steve Aug 21 '14 at 14:56
  • possible duplicate of [WPF: ListBox with WrapPanel, vertical scrolling problem](http://stackoverflow.com/questions/818876/wpf-listbox-with-wrappanel-vertical-scrolling-problem) – Steve Aug 21 '14 at 14:57

1 Answers1

3

It seems as though you could do with reading through the Panels Overview page on MSDN. Different Panels have different behaviours and it's pretty important that all new WPF developers know the differences. For example, a Grid can automatically resize its content (the controls placed inside it), whereas a WrapPanel will not.

Therefore, when wishing to constrain the dimensions of your control(s), you should put them into a Grid, or one of the other Panels that provide that functionality and avoid the ones that don't.

You said:

I've tried to set the Height of the grid row to *, but it didn't help

That's because your ListBox is not in a Grid... its parent WrapPanel may be in the Grid, so you could try to set the Grid.Row on that instead.

Sheridan
  • 68,826
  • 24
  • 143
  • 183