计算DataGrid中可见行的数量

Ami*_*nen 5 .net c# wpf datagrid .net-4.0

我想知道WPF实际显示了多少行DataGrid.

我尝试循环DataGridRow并检查IsVisible,但似乎行报告,IsVisible = true即使它们不在DataGrid视口中.

如何正确计算可见行数?

Ami*_*nen 2

我也在 MSDN 论坛上问过这个问题并得到了很好的答案

private bool IsUserVisible(FrameworkElement element, FrameworkElement container) {
    if (!element.IsVisible)
        return false;
    Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight));
    Rect rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
    return rect.Contains(bounds.TopLeft) || rect.Contains(bounds.BottomRight);
}
Run Code Online (Sandbox Code Playgroud)