seanzi
seanzi

Reputation: 883

wpf intelligent grid layout

I am currently working on a project which contains a window, that has a need to display between 1 and 9 user controls.

I am looking for the best way to ensure that the full space available is used by the number of user controls selected.

e.g. if 9 items are selected then a grid of 3 x 3 is displayed. However if 8 are selected then 2 rows of 3 would be shown with a row of 2 below. (using a colspan).

I have been trying to use a uniform grid as this looks as though most of the desired behaviour already exists. (displays items in the ordered added, auto resizes to window size). However I'm struggling to implement some form of intelligence when the number of items selected is not divisible by 3!

Has anybody encountered this problem before? Or can suggest a suitable way to solve the problem?

Thanks!!

Upvotes: 1

Views: 649

Answers (2)

Rachel
Rachel

Reputation: 132678

Try using a WrapPanel and setting the Width of each object to 1/3 of the wrap panel's size.

This will automatically move items to a new row when the first row fills up

Upvotes: 1

Dave
Dave

Reputation: 15016

Stick with the uniform grid. Since it sounds like you want 3 columns, regardless of the number of controls displayed, you just have to use:

<UniformGrid Columns="3" />

And it will do what you've asked for -- if you have 8 controls, you'll get 2x3 + 2 extra below.

Upvotes: 0

Related Questions