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

查看:125
本文介绍了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 行当前未显示。

例如,如果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?

推荐答案

使用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天全站免登陆