A list of UIElements with the same height are added to a WrapPanel
, and the XAML is
<Grid>
<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView >
</Grid>
When run it looks like this
In order to get the item count of the first row, I tried to calculate the coordinates (relative to the listview) of each item's upper-left corner, by comparing each item's vertical offset with the first item, I can get the item count of the first row.
But the code can easily break if items have different height.
ListView listView = (sender as ListView);
if (listView.Items.Count > 0)
{
UIElement firstItem = ((listView.Items)[0] as UIElement);
double y = firstItem.TranslatePoint(new Point(0, 0), listView).Y;
int counter = 0;
foreach (UIElement item in listView.Items)
{
if ((item.TranslatePoint(new Point(0, 0), listView).Y != y))
{
break;
}
counter++;
}
}
What is the correct way to do this?