tags:

views:

43

answers:

1

I'm just starting out with WPF and I'm trying to layout a UI.

I have a StackPanel and within it's bounds I want two listboxes stacked vertically, each covering half of the container panel - regardless of what contents are in them. So far, the listboxes are resizing depending in the items in them, so when they are empty, they don't cover any of the container panel, and as items are added they resize and look pretty ugly.

In the old Winforms days I'd trap a resize event and set the height manually. I know that's not the "right" way anymore, but I'm not sure what the right way is!

So, any suggestions?

+2  A: 

Use a grid with one column and two rows, instead of the stack panel.
Don't set the row height to anything, by default it it will share the height equally between any rows.

Each row will occupy 50% of the grid height. Add the list boxes to col=0, row=0 & col=0, row=1 on the grid.

If you want to change the porportion, say 1/3 & 2/3 Set one row height to "2*" and the other to "1*"

Hope this helps.

Binary Worrier
Should be `1*` as `*` is the unit, but the `1` could be omitted in that case, too.
Joey
Why do you suggest setting the row height to anything? By default they would be alotted half the height of the grid. +1 for Grid, tho, as it is the proper container.
Will
Binary Worrier
Thanks for the quick answer! You mean like this ? <Grid ShowGridLines="True" > <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> </Grid.ColumnDefinitions> <ListBox Grid.Row="0" > ... </ListBox> <ListBox Grid.Row="1" > ... </ListBox> </Grid>Unfortunately, the listboxes are still growing and shrinking depending on there contents. :(
Paul
Did you put the grid itself in a StackPanel? Are you using a Canvas? Either of these could be the problem. Try making the grid the main element in the Window.
YotaXP
Yep, I did, that was the problem. Fixed now, thanks!
Paul