Question

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

enter image description here

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?

Was it helpful?

Solution

This method should get the job done:

private static int GetItemsInFirstRow(ItemsControl itemsControl)
{
    double previousX = -1;
    int itemIndex;

    for (itemIndex = 0; itemIndex < itemsControl.Items.Count; itemIndex++)
    {
        var container = (UIElement)itemsControl.ItemContainerGenerator
                                                .ContainerFromIndex(itemIndex);

        var x = container.TranslatePoint(new Point(), itemsControl).X;

        if (x <= previousX)
        {
            break;
        }

        previousX = x;
    }

    return itemIndex;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top