views:

691

answers:

2

I'm using a WrapPanel as the ItemsPanel of an ItemsControl. Right now, the items in the control wrap like this:

|1234567  |
|890      |

I'd like them to wrap like this:

| 1234567 |
|   890   |

Conceptually, the layout process should align each line of items such that it's centered within the WrapPanel's bounds.

Can someone explain how this is possible please?

+4  A: 

Unfortunately the stock WrapPanel will not do what you're asking for, because its intention is to fill all available space horizontally (or vertically) before wrapping. You will probably have to design your own WrapPanel to handle this case. You could start with this sample that shows how to create your own WrapPanel.

sixlettervariables
+2  A: 

The simple answer is that you can't center align the contents of a WrapPanel. You can center align the panel itself, but the last line will still be left aligned within the panel.

Alterate suggestions:

Use Grid with rows and columns. If you are not adding items to the collection dynamically, this could work nicely.

Create your own version of the WrapPanel that works the way you need it to. This MSDN document describes how panels work and includes a section on creating custom panels. It also has a link to a sample custom panel.

Josh G