开发者

Very basic WPF layout question

开发者 https://www.devze.com 2022-12-12 14:17 出处:网络
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\"

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 dyna开发者_StackOverflowmicly 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消