问题描述:

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!

网友答案:

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>
网友答案:

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.

相关阅读:
Top