Home > Back-end >  Rows Property of UniformGrid
Rows Property of UniformGrid

Time:04-01

I am new to WPF and trying to understand how to use a UniformGrid: A uniform grid with 3 rows and 4 columns displaying 11 items (the last cell is empty).

Based on the documentation on UniformGrid, I had expected that I would get 1 row with 3 items uniformly laid out and the other 7 items would not display. Did I misunderstand the documentation or am I doing something incorrectly that is causing additional rows to display?

CodePudding user response:

First of all, you are refering to the wrong documentation, yours is for UWP, not WPF.

The behavior should be the same, but it is not explicitly stated in the referenced documentation for WPF. However, there seems to be an issue that stems from setting VerticalAlignment to Center and is not related to the ItemsControl, it will be the same for an isolated UniformGrid.

Whenever the UniformGrid contains more than the maximum number of items it can display (Rows x Columns) and the VerticalAlignment is set to any other value than the default Stretch, all of the items are displayed regardless of the number of rows, but respecting the number of columns.

What you could do is remove the VerticalAlignment and try to compensate for it by aligning the ItemsControl in a way that it fits your original intent.

<ItemsPanelTemplate>
   <UniformGrid HorizontalAlignment="Stretch" Rows="1" Columns="3"/>
</ItemsPanelTemplate>
  • Related