Getting No of rows in Uniformgrid

210 Views Asked by At

I have set uniformgrid as itemscontrol ItemsPanel.Havent set the no of rows or column.It adjusts according to the screensize.Now is it possible to get the no of rows?

1

There are 1 best solutions below

3
On

When you ask a question here on StackOverflow, you really should provide more information. Had you specified whether you wanted to access the number of rows in code behind or in a view model (the answers would be different), I'm guessing that you would have already received an answer.

I'm guessing that you want to access them in the code behind... for this method you'll need to implement this method:

public T FindVisualChild<T>(DependencyObject dependencyObject) where T : 
DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(dependencyObject, i);
        if (child != null && child is T) return (T)child;
        else
        {
            T childOfChild = FindVisualChild<T>(child);
            if (childOfChild != null) return childOfChild;
        }
    }
    return null;
}

Then using this method:

UniformGrid uniformGrid = FindVisualChild<UniformGrid>(ItemsControl);

This example uses this XAML:

<ItemsControl Name="ItemsControl">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="3" Rows="2" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>