Search code examples
wpflayoutuniformgrid

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!!


Solution

  • 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