I am using a copy of the WrapPanel code mentioned in an earlier post. The wrap panel measures UIElement items and then adds columns as needed to hold all objects at their desired size.

The flaw in the existing WrapPanel code is that there is no way to resize an item to fill all available space. I accomplished this by making a few changes to the wrappanel.cs code. The changes can be seen in the code below:

Any item that I want to be adjustable must have its its MinHeight value set to a non-zero value. This appears to only work with a Grid element with at least one row set to “*”. Without the MinHeight value, the code would not recognize that it can adjust the size of this item. For a UserControl derived class, the user control itself must have the MinHeight value set, not the Grid within it.

Note that this does not work with a StackPanel because the StackPanel will not shrink any item in it if it is shrunk.

The code does a few things. First it lets the item measure itself. Then it detects that there is a MinHeight value set, and in anticipation of the item fitting in the space, sets the item height to the remaining column space. I could have moved this code later and not done this twice, but I just wrote it and don’t want to experiment anymore tonight.

Anyhow, the code then detects if the item is too tall to fit. The Item.DesiredSize.height value will be the minimum size when the test is performed. If it is too tall, The item is moved to the next column. The item is then resized and re-measured using the full column height.

I have not published the user control changes needed for this to work. Let me know if you want to see that XAML file, and I’ll post it.

Dave