It seems that there are spacing between each item container, also margin and padding in the container. My question is how to calculate the actual size of a ItemContainer in GridView so that I can calculate how many the rows the grid would accommodate. What I can get are each ItemTemplate’s height, and the grid’s height. Given those values, how to calulate?
It seems that there are spacing between each item container, also margin and padding
Share
If you create a template Grid App, add a NuGet reference to winrtxamltoolkit debugging components and add a breakpoint in the Standard250x250ItemTemplate by setting
where
you will see the visual tree trace like this in the output window of VS:
Now since that item template uses a 250×250 Grid as its root and the GridView is using the default style with the default ItemContainerStyle – it seems like the default GridViewItem style has one Border called ContentBorder that defines a Margin of “4”. Depending on how your grid items are configured you can use that knowledge to calculate the size you are looking for in different ways.
If you define a constant size ItemTemplate – like the 250×250 one in the Grid App project template – you end up with items that occupy 258×258 squares of space. If you change the ItemContainerStyle to change the Margins around each ContentBorder – you would have to adjust your calculations. If you don’t specify constant size for your ItemTemplate – your item sizes will be based on the first item added to the GridView.
You can use Blend to analyze the control templates in more details or use the VisualTreeDebugger, XAML Spy or your own code that walks the visual tree using VisualTreeHelper to learn more about the layout.