Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify the max number of columns for a WrapPanel in WPF

Tags:

I have a WrapPanel, And I want to specify the Max number of its columns. So, for example, when my Collection "ObjectCollection" (binded to this WrapPanel) contains only 4 elements, the WrapPanel will have only one row. But, when "ObjectCollection" will have 5 elements, the wrapPanel will create another row to put the fifth one. (My Max_Columns_Number in this case is 4).

like image 369
NTinkicht Avatar asked Sep 05 '13 11:09

NTinkicht


People also ask

How to use WrapPanel in wpf?

WPF WrapPanel control is a panel that positions child elements in sequential position from left to right by default. If child elements that are stacked don't fit in the row or column they are in, the remaining elements will wrap around in the same sequence.

What is uniform grid?

The UniformGrid control is a responsive layout control which arranges items in a evenly-spaced set of rows or columns to fill the total available display space. Each cell in the grid, by default, will be the same size.


1 Answers

I'm pretty sure you can't do it with a WrapPanel, but you can use the UniformGrid instead.

That one has properties to specify the number of rows and columns you want.

If you set the Columns property to 4, it will keep 4 items in each row, and then wrap to the next one.

<UniformGrid Columns="4">
    <!-- In first row -->
    <Button Content="test"></Button>
    <Button Content="test"></Button>
    <Button Content="test"></Button>
    <Button Content="test"></Button>

    <!-- In second row -->
    <Button Content="test"></Button>
</UniformGrid>
like image 68
Peter Hansen Avatar answered Oct 04 '22 16:10

Peter Hansen