DataGrid ScrollIntoView - 如何滚动到未显示的第一行?

时间:2022-09-17 14:40:41

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

我试图向后滚动一个WPF DataGrid代码。我用

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]);
}

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

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

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.

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

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?

我希望实现这一点,当我调用此方法时,网格将下一行放入网格的顶部(如滚动条所做)。为什么它不起作用?

2 个解决方案

#1


18  

Use DataGridView.FirstDisplayedScrollingRowIndex.

使用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;
    }

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

抱歉没有意识到WPF网格没有那个。关于滚动的观点保持有效。

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.

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

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]);

#2


2  

Check this out, it's for a ListBox but the insight is great and it may also work for the grid:

看看这个,它是一个ListBox,但洞察力很好,它也可能适用于网格:

In a few words: the items are loaded into the ListBox asynchronously, so if you call ScrollIntoView() within the CollectionChanged event (or similar) it will not have any items yet, so no scrolling.

简而言之:项目是异步加载到ListBox中的,所以如果你在CollectionChanged事件(或类似)中调用ScrollIntoView(),它将没有任何项目,所以不滚动。

Hope it helps, it surely helped me! ;-)

希望它有所帮助,它肯定对我有帮助! ;-)

#1


18  

Use DataGridView.FirstDisplayedScrollingRowIndex.

使用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;
    }

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

抱歉没有意识到WPF网格没有那个。关于滚动的观点保持有效。

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.

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

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]);

#2


2  

Check this out, it's for a ListBox but the insight is great and it may also work for the grid:

看看这个,它是一个ListBox,但洞察力很好,它也可能适用于网格:

In a few words: the items are loaded into the ListBox asynchronously, so if you call ScrollIntoView() within the CollectionChanged event (or similar) it will not have any items yet, so no scrolling.

简而言之:项目是异步加载到ListBox中的,所以如果你在CollectionChanged事件(或类似)中调用ScrollIntoView(),它将没有任何项目,所以不滚动。

Hope it helps, it surely helped me! ;-)

希望它有所帮助,它肯定对我有帮助! ;-)