当 PageUp/PageDown 为 Datagrid 时 - ICollectionView 如何工作?

发布于 2024-11-14 20:06:35 字数 1729 浏览 1 评论 0原文

我有一个艰巨的任务要解决。使用 WPF Datagrid 时,只要没有任何分组,分页就可以正常工作。

然而,一旦分组出现,事情就变得有点棘手。事实上,向上/向下翻页不再正常工作。

我发现的一种解决方法是使用以下内容访问作为 dataGrid 内部包装器的内部滚动查看器:

helper class:

public static class VisualTreeUtilities
    {
        public static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        } 
}

实际代码: ctor:

_scrollViewer2 = VisualTreeUtilities.GetVisualChild<ScrollViewer>(this.dataGrid);


private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.PageUp:
                    _scrollViewer2.PageUp();
                    e.Handled = true;
                    break;
             }
         }

这样我就可以完美地滚动,尽管使用了分组。剩下的唯一问题是,当向下分页时,Datagrid 不会将实际的 selectedItem 向下,或在向上分页时向上。翻页时,选定的单元格保留在以前的位置并且在视线之外。我希望当所选单元格保留在那里时,它就像 Excel 一样。这意味着当选择第三行的第二个单元格并向下翻页时,仍然会选择第三行的第二个单元格。我怎样才能在 WPF 数据网格中做这样的事情?

我需要了解它的内部运作方式。 ListCollectionView 如何知道要向下翻页多少行?如果我知道它如何计算行数,我可以使用相同的原理来计算行数并将 selectedItem 设置到该位置并执行 cell.focus() 并解决问题。

有人能帮助我理解缺失的部分吗?

非常感谢,

I have a difficult task to solve. When using WPF Datagrid the Paging works fine as long as you don't have any groupings.

However as soon as groupings come into the place, it becomes a bit tricky. In fact The pageup/down don't work correctly anymore.

One workaround I have found is to access the internal scroll viewer that is wrapper inside the dataGrid by using this:

helper class:

public static class VisualTreeUtilities
    {
        public static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        } 
}

Actual code:
ctor:

_scrollViewer2 = VisualTreeUtilities.GetVisualChild<ScrollViewer>(this.dataGrid);


private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.PageUp:
                    _scrollViewer2.PageUp();
                    e.Handled = true;
                    break;
             }
         }

This way I can perfectly scroll fine despite using the groupings. The only problem that remains is that Datagrid doesn't take the actual selectedItem with it downwards when paging down, or upwards when paging up. The selected cell remains at the former position and out of sight when paging away. I wished it was like excel when the selected cell remains there. It means when the second cell of third row is selected and you page down, still the second cell of the third row would be selected. How could I do something like this in a WPF datagrid?

I need to understand how internally it works. How does the ListCollectionView know how many rows to page down? if I knew how it counts down the rows, I could use the same principle to count down the rows and set the selectedItem to that position and do a cell.focus() and problem solved.

Can anybody help me with understanding the missing bit?

Many Thanks,

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文