tags:

views:

9653

answers:

4

Hey,

Didn't get a reply to this on the MSDN WPF forum, so I thought I'd try here.

Some WPF controls - like the button - seem to happily consume all the availible space in its' container if you don't specify the height it is to have.

And some - like the ones I need to use right now, the (multiline) TextBox and the ListBox - seem more worried about just taking the space necessary to fit their contents, and no more.

If you put these guys in a cell in a UniformGrid, they will expand to fit the available space. However, uniformgrids are not right for all situations. What if you have a grid with some rows set to a * height to divide the height between itself and other * rows? What if you have a StackPanel and you have a label, a list and a button - how can you get the list to take up all the space not eaten by the label and the button?

I would think this would really be a basic layout requirement, but I can't figure out how to get them to fill the space that they could (putting them in a DockPanel and setting it to fill also doesn't work, it seems, since the DockPanel only takes up the space needed by its' subcontrols..

I have tried to search for the answer to this, and I am guessing the answer must be out there, as a resizable GUI would be quite horrible if you had to play with Height/Width and MinHeight/MinWidth etc.

Can you bind your height and width to the grid cell you occupy? Or is there another way to do this?

Very grateful for any insights!

EDIT: Could only accept one answer, so I gave it to the guy with the least points already. Hope that's ok. :)

Rune

+1  A: 

Well, I figured it out myself, right after posting, which is the most embarassing way. :)

It seems every member of a StackPanel will simply fill it's minimum requested size.

In the DockPanel, I had docked things in the wrong order. If the TextBox or ListBox is the only docked item without an alignment, or if they are the last added, they WILL fill the remaining space as wanted.

I would love to see a more elegant method of handling this, but it will do.

Rune Jacobsen
+5  A: 

There are also some properties you can set to force a control to fill its available space when it would otherwise not do so. For example, you can say:

HorizontalContentAlignment="Stretch"

... to force the contents of a control to stretch horizontally. Or you can say:

HorizontalAlignment="Stretch"

... to force the control itself to stretch horizontally to fill its parent.

Matt Hamilton
The most notorious panel mentioned in all of these cases is the StackPanel. It does not contain ContentAlignment properties and setting its children to Stretch will have no effect inside of a star-sized grid. Very frustrating. It's almost as if the StackPanel was the first container the WPF team wrote and it suffers for that being the case.
Brent Schooley
@Brent - ahh! I've been stuck on trying to get my stackpanel's contents fill correctly. thanks! I thought it was just something I was doing very wrong.
townsean
+10  A: 

Each panel control implements distinct layout logic performed in Measure() and Arrange(). Measure determines the size of the panel and each of its children. Arrange() determines the rectangle where each control renders.

The last child of the DockPanel fills the remaining space. You can disable this behavior by setting the LastChild property to false.

The stack panel asks each child for its desired size and then stacks them. The stack panel calls Measure() on each child with an available size of infinity and then uses the child's desired size.

A Grid occupies all available space, however, it will set each child to their desired sizes and then center them in the cell.

You can implement your own layout logic by deriving from Panel and then over riding MeasureOverride and ArrangeOverride.

See this article for a simple example:

http://msdn.microsoft.com/en-us/library/ms753321.aspx

A: 

Use the HorizontalAlignment and VerticalAlignment layout properties. They control how an element uses the space it has inside its parent when more room is available than it required by the element.

The width of a StackPanel, for example, will be as wide as the widest element it contains. So, all narrower elements have a bit of excess space. The alignment properties control what the child element does with the extra space.

The default value for both properties is Stretch, so the child element is stretched to fill all available space. Additional options include Left, Center and Right for HorizontalAlignment and Top, Center and Bottom for VerticalAlignment.

urini
If this was always true, the original question would not have been asked. Take, for instance, a horizontal StackPanel with a Label and a TextBox. There is extra space to the right of the TextBox. The TextBox is set to Auto width. Setting Stretch for HorizontalAlignment will not make the TextBox fill the remaining space.
Brent Schooley