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

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

问题描述

我正在尝试向下滚动一个带有代码的WPF数据网格。
我使用

  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)才能滚动到下一行。



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



我想要实现这一点,当我调用这个方法时,网格将带来下一行到网格的顶部(像滚动器)。
为什么不工作?

解决方案

使用DataGridView.FirstDisplayedScrollingRowIndex。

  int itemNum = 0; 
private void Down_Click(object sender,RoutedEventArgs e)
{
itemNum ++;
if(itemNum> dataGridView.Items.Count - 1)return;
//dataGridView.UpdateLayout(); < - 我不认为你需要这个
dataGridView.FirstDisplayedScrollingRowIndex = itemNum;
}


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



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



这项工作将适用于您。您滚动到最下面的一行,然后向上滚动到您需要的任何行。注意,这里你实际上需要更新布局,否则会忽略第一个滚动的结果,然后再次滚动。

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


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

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

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.

 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.

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

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

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