I have a DataGrid
of variable dimensions dependent upon screen-res. I need to know how many rows are visible to the user. Here's my code:
uint VisibleRows = 0;
var TicketGrid = (DataGrid) MyWindow.FindName("TicketGrid");
foreach(var Item in TicketGrid.Items) {
var Row = (DataGridRow) TicketGrid.ItemContainerGenerator.ContainerFromItem(Item);
if(Row != null && Row.IsVisible) {
VisibleRows++;
}
}
I'm using the following code to test the vars:
MessageBox.Show(String.Format("{0} of {1} rows visible", VisibleRows, TicketGrid.Items.Count));
I can't just - 1
, because it's only incorrect after a certain number have been added. I can't check > 10
, because the dimensions are variable.
How can I fix this?
Here's what finally worked for me:
uint VisibleRows = 0;
var TicketGrid = (DataGrid) MyWindow.FindName("TicketGrid");
foreach(var Item in TicketGrid.Items) {
var Row = (DataGridRow) TicketGrid.ItemContainerGenerator.ContainerFromItem(Item);
if(Row != null) {
/*
This is the magic line! We measure the Y position of the Row, relative to
the TicketGrid, adding the Row's height. If it exceeds the height of the
TicketGrid, it ain't visible!
*/
if(Row.TransformToVisual(TicketGrid).Transform(new Point(0, 0)).Y + Row.ActualHeight >= TicketGrid.ActualHeight) {
break;
}
VisibleRows++;
}
}
Upto and including row 9 shows 9 of 9 visible. The "half-visible" row 10 results in 9 of 10 visible. It's actually better for my purposes for this not to count as a visible row, so this'll do for me! :)
Note: if you're reusing my code without using the break
, any invisible rows after the offending row will throw a NullRefException
.