views:

179

answers:

2

Hi All,

<StackPanel>
     <GroupBox>
                    <StackPanel Orientation="Horizontal">
                        <ListBox></ListBox>
                    </StackPanel>
     </GroupBox>
</StackPanel>

How do I get this listbox to be maxiumum size, i.e. fill the group box its in. I've tried width="auto" Height="auto", width="stretch" Height="stretch"

annoyingly at the moment, it dynamicly sizes to whatever fills it - (i cant think of any situation you'd want a listbox to do that but anyway) - or I can only statically set its size.

Cheers!

+1  A: 

If you want sizing to fill, you should be using Grids not StackPanels.

<Grid>
  <GroupBox>
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="*" />
      </Grid.RowDefinitions>
      <ListBox />
    </Grid>
  </GroupBox>
</Grid>

If you really want StackPanels, you can use HorizontalContentAlignment="Stretch" and VerticalContentAlignment="Stretch" on the StackPanel.

vanja.
+1  A: 

The problem is that StackPanels try to take up as little space as possible.

Just remove them and you should be fine.

<GroupBox>
    <ListBox />
</GroupBox>

If that doesn't fit your situation, we'll need a bit more context on where the ListBox is going.

DockPanels are also good for making controls use up the remaining space. The last item inside a DockPanel uses all the remaining space (as long as you haven't set LastChildFill = false).

The below example allows the TextBlock to take up as much space as it needs at the top, and then the GroupBox with the ListView takes up the rest.

<DockPanel>
    <TextBlock DockPanel.Dock="Top" Text="My TextBlock Text" />
    <GroupBox>
        <ListBox />
    </GroupBox>
</DockPanel>
Ray
the problem I have with that is that that "overflows" the listbox spills out under the form?
baron
ah. all good. had to remove height=auto width=auto etc.thank you!
baron