如何在 WPF 数据网格上自动滚动 [英] How to autoscroll on WPF datagrid

查看:30
本文介绍了如何在 WPF 数据网格上自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我很傻.我现在搜索了 15 分钟,找到了几种不同的在数据网格上滚动的解决方案,但似乎没有一个适合我.

I think I am stupid. I searched now for 15 minutes, and found several different solutions for scrolling on datagrids, but none seems to work for me.

我将 WPF 与 .NET 3.5 和 WPF Toolkit DataGrid 一起使用.当我的可观察集合更改时,我的网格会更新,完美运行.现在,我的 DataGrid 位于普通 Grid 内,如果 DataGrid 变得太大,则会出现滚动条.也不错...

I am using WPF with .NET 3.5 and the WPF Toolkit DataGrid. My grid gets updated when my observable collection changes, works perfectly. Now, my DataGrid is located inside a normal Grid and scrollbars appear if the DataGrid gets too big. Also fine...

现在是 1.000.000 美元的问题:

And now comes the 1.000.000 $ question:

如何让数据网格滚动到最后一行?有:

How do I get the datagrid to scroll to the last row? There is:

  • 没有 AutoScroll 属性
  • 没有 CurrentRowSelected 索引
  • 一个 CurrentCell,但没有我可以用于 CurrentCell = AllCells.Last 的集合

有什么想法吗?我真的觉得自己很傻,这个问题这么难似乎也很奇怪.我错过了什么?

Any ideas? I feel really stupid, and it seems strange that this question is so hard. What am I missing?

推荐答案

;)

if (mainDataGrid.Items.Count > 0)
{
    var border = VisualTreeHelper.GetChild(mainDataGrid, 0) as Decorator;
    if (border != null)
    {
        var scroll = border.Child as ScrollViewer;
        if (scroll != null) scroll.ScrollToEnd();
    }
}

这篇关于如何在 WPF 数据网格上自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆