locked
Listbox in a stackpanel RRS feed

  • Question

  • I have a stackpanel (vertical) with a listbox in it.
    If I put a number of items in the listbox, the listbox grows as wel as the stackpanel.
    The listbox doesn't get vertical scrollbars. The stackpanels height gets bigger then the height of his parent.

    How do I prevent the stackpannel from growing bigger than the parent, so the listbox gets scrollbars?


     

        <Grid x:Name="LayoutRoot" Background="White">
            <StackPanel Orientation="Vertical" VerticalAlignment="Stretch">
                <Button>Just a button</Button>
                <ListBox>
                    <ListBox.Items>
                        <ListBoxItem>This is a listbox item</ListBoxItem>
                          .
                          .
                          .
                        <ListBoxItem>This is a listbox item</ListBoxItem>
                    </ListBox.Items>
                </ListBox>
                <Button>Just a button</Button>
            </StackPanel>
        </Grid>
    
     
    Monday, June 7, 2010 9:32 AM

Answers

  •  My listbox had a scroll bar because I had 20+ items in the collection source. Also I was just changing the height of the accordion, it should be fine whatever the height is. Let me know if its working and if not we can try to find a resolution.

    Tuesday, June 8, 2010 9:12 AM

All replies

  • You can set the height or MaxHeight property on either control or both:

    <ListBox MaxHeight="100">

    Or use a grid instead of a stackpanel

     

    Monday, June 7, 2010 9:37 AM
  • This was not what I was looking for.

    The actual problem is the stackpannel used in the accordion control.
    I have a accordion control following the height of the browser. In each accodion item is a listbox causing this problem

     

    Monday, June 7, 2010 9:44 AM
  • So you need the list box size to change dynamically based on the accordion size?

    Or why is it that you can't use maxHeight?

    Monday, June 7, 2010 10:08 AM
  • The accordion resizes if the browser window resize, so the maxHeight is no option.

    The accordions size is depending on the browser size
    The listbox size is depending on the available size inside the accordion.

    Monday, June 7, 2010 11:26 AM
  • Replace the StackPanel with a Grid.  The StackPanel doesn't provide the appropriate height constraint that you need for the ListBox's ScrollViewer.  If you need to have multiple items in the vertical layout, then you can define the Grid's RowDefinitions as long as you ensure that the ListBox gets the "remaining area".

    Monday, June 7, 2010 11:45 AM
  •  As I stated initially, you can also use a grid. This works perfectly fine:

     

    <toolkit:Accordion Height="500" Margin="8,190,0,0" HorizontalAlignment="Left" Width="388">
    			<toolkit:AccordionItem Header="Header">
    				 <Grid x:Name="grid" Background="White">
    			        <Grid.RowDefinitions>
    			        	<RowDefinition Height="Auto"></RowDefinition>      
    						<RowDefinition></RowDefinition>  
    						<RowDefinition Height="Auto"></RowDefinition>  
    					</Grid.RowDefinitions>
    			            <Button>Just a button</Button>
    			            <ListBox Grid.Row="1" ItemTemplate="{StaticResource ItemTemplate1}" ItemsSource="{Binding Collection}"/>
    			            <Button Grid.Row="2">Just a button</Button>			        
        			</Grid>
    			</toolkit:AccordionItem>
    			<toolkit:AccordionItem Content="Content" Header="Header"/>
    			<toolkit:AccordionItem Content="Content" Header="Header"/>
    		</toolkit:Accordion>
      
    Monday, June 7, 2010 1:36 PM
  • @devdmans: 

    This works partially because the accordion has a fixed height.
    What if the accordion is verticaly stretched to its parent.
    There's also no scrollbar in the listbox.

    Tuesday, June 8, 2010 2:47 AM
  •  My listbox had a scroll bar because I had 20+ items in the collection source. Also I was just changing the height of the accordion, it should be fine whatever the height is. Let me know if its working and if not we can try to find a resolution.

    Tuesday, June 8, 2010 9:12 AM