计算 DataGrid 中可见行的数量

发布于 2024-11-06 23:53:56 字数 204 浏览 0 评论 0 原文

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

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

如何正确计算可见行数?

I would like to know how many rows are actually displayed by a WPF DataGrid.

I tried looping over DataGridRow and checking IsVisible, but it seems that rows report IsVisible = true even when they are not in the DataGrid viewport.

How can I count the number of visible rows correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

戒ㄋ 2024-11-13 23:53:56

我也在 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);
}

I've asked this question also on MSDN forum and got a good answer:

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);
}
゛清羽墨安 2024-11-13 23:53:56

我遇到了同样的问题,行显示为 Visible = true 即使它们不是。

试图想出一个解决方案,我发布了这个问题: DataGrid 中的可见行减少 1(使用 ContainerFromItem 进行计数)

这对我有用:

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) {
        if(Row.TransformToVisual(TicketGrid).Transform(new Point(0, 0)).Y + Row.ActualHeight >= TicketGrid.ActualHeight) {
            break;
        }

        VisibleRows++;
    }
}

为了获得进一步的指导,我对链接问题的回答中有一些 /* comments */ ,以及对问题本身的用户评论线索,这些评论导致了答案。

I had the same problem with rows showing as Visible = true even when they weren't.

Trying to come up with a solution, I posted this question: Visible rows in DataGrid is off by 1 (counted using ContainerFromItem).

Here's what 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) {
        if(Row.TransformToVisual(TicketGrid).Transform(new Point(0, 0)).Y + Row.ActualHeight >= TicketGrid.ActualHeight) {
            break;
        }

        VisibleRows++;
    }
}

For further guidance, there are some /* comments */ in my answer on the linked question, as well as a thread of user comments on the question itself that led to the answer.

分开我的手 2024-11-13 23:53:56

我想到了一个简单的技巧,

循环遍历所有行并检查项目是否有容器?

dataGrid.GetContainerFromItem(dataGrid.Items[row]);

希望这有帮助

a simple hack come to mind,

loop over all rows and check if item has a container?

dataGrid.GetContainerFromItem(dataGrid.Items[row]);

hope this helps

失退 2024-11-13 23:53:56

如果您需要另一个 xaml 元素,只需通过“ElementName”添加引用,并通过“Items.Count”添加属性到您的内容属性(在本例中为“Text”)。您还可以使用转换器来解析该值。

<TextBlock Text="{Binding ElementName=ComponentDataGrid, Path=Items.Count, Converter={StaticResource IntToStringConverter}}"/>

If you need it for another xaml element just add a reference via "ElementName" and the property via "Items.Count" to your content property(in this case "Text"). You might also use a converter to parse the value.

<TextBlock Text="{Binding ElementName=ComponentDataGrid, Path=Items.Count, Converter={StaticResource IntToStringConverter}}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文