0

I want to create my xaml Grid element from the amount of elements

i.e. if I have 6 elements I'll have to do 3 columns and 2 rows (or vice versa)

But my amount of elements (input) can be any value, can be 7, 8, 19, etc.

How can I determinate how many columns and rows I need?

EDIT: More information: I want my grid as square as possible, doesn't matter the possible empty spots

4

1 回答 1

1

使用Math.Sqrt

int nElements = 6;
int nColumns = (int)Math.Ceiling(Math.Sqrt(nElements));
int nRows;
if (nElements <= nColumns*(nColumns-1)) // last row remains empty
    nRows = nColumns-1;                 // eliminate it
else
    nRows = nColumns;

您可以尝试使用nElements. 它会做得很好!:)

于 2013-08-22T19:37:46.763 回答