0

我已将 uniformgrid 设置为 itemscontrol ItemsPanel.Havent 设置行数或列数。它根据屏幕大小进行调整。现在可以获得行数吗?

4

1 回答 1

0

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>
于 2013-10-30T09:49:51.330 回答