DataGrid ScrollIntoView - 如何滚动到未显示的第一行? [英] DataGrid ScrollIntoView - how to scroll to the first row that is not shown?

查看:24
本文介绍了DataGrid ScrollIntoView - 如何滚动到未显示的第一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向下滚动带有代码的 WPF DataGrid.我用

I am trying to scroll down a WPF DataGrid with code behind. I use

int itemNum=0;
private void Down_Click(object sender, RoutedEventArgs e)
{
    if (itemNum + 1 > dataGridView.Items.Count - 1) return;
    itemNum += 1;
    dataGridView.UpdateLayout();
    dataGridView.ScrollIntoView(dataGridView.Items[itemNum]);
}

仅当 itemNum 行当前未显示时才会向下滚动.

This scrolls down only if the itemNum row is not currently shown.

例如,如果 DataGrid 的长度足以容纳 10 行,而我有 20 行,我需要调用此函数 11 次(直到 itemNum 为 11)才能滚动到下一个行.

For example, If the DataGrid is long enough to hold 10 rows and I have 20 rows, I need to call this function 11 times (untill itemNum is 11) in order to scroll to the next row.

如果该行已经适合网格(即使它是屏幕上的最后一行),则不会向下滚动.

It doesnt scroll down if the row is already fits in the grid (even if its the last on the screen).

我想实现这一点,当我调用这个方法时,网格会将下一行带到网格的顶部(就像滚动条一样).为什么它不起作用?

I want to achieve that when I call this method , the grid will bring the next line into the top of the grid (as the scroller does). Why isnt it working?

推荐答案

Use DataGridView.FirstDisplayedScrollingRowIndex.

Use DataGridView.FirstDisplayedScrollingRowIndex.

 int itemNum=0;
    private void Down_Click(object sender, RoutedEventArgs e)
    {
        itemNum++;
        if (itemNum > dataGridView.Items.Count - 1) return;
        //dataGridView.UpdateLayout();  <-- I don't think you need this
        dataGridView.FirstDisplayedScrollingRowIndex = itemNum;
    }

抱歉没有意识到 WPF 网格没有那个.关于滚动的观点仍然有效.

Sorry didn't realize WPF grid didn't have that. The point about scrolling stays valid tho.

ScrollIntoView 仅在该项目不在视图中时才会滚动,如果它位于当前可见行下方,则会使其成为最后一行,因此当您滚动查看第 11 个项目时,它看起来像是滚动到第二个项目.

ScrollIntoView will only scroll if the item is not in view, and will make it the last row if it is below the current visible lines, thus when you scroll in to view the 11th item it looks like it scrolls to the second.

这个变通办法应该适合你.您滚动到最底部的行,然后向上滚动到您需要的任何行.请注意,这里您实际上需要更新布局,否则它会在再次向上滚动之前忽略第一次滚动的结果.

This work around should work for you. You scroll to the bottom most row and then scroll up to whatever row you need. Note, here you actually need to update layout or it will ignore results of the first scroll before scrolling up again.

        dataGridView.ScrollIntoView(DataGrid1.Items[DataGrid1.Items.Count - 1]);
        dataGridView.UpdateLayout();
        dataGridView.ScrollIntoView(DataGrid1.Items[itemIndex]);

这篇关于DataGrid ScrollIntoView - 如何滚动到未显示的第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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