C# DataGrid ScrollIntoView - 如何滚动到未显示的第一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9667475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGrid ScrollIntoView - how to scroll to the first row that is not shown?
提问by Programer
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 itemNumrow 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 itemNumis 11) in order to scroll to the next row.
例如,如果 DataGrid 足够长以容纳 10 行,而我有 20 行,我需要调用此函数 11 次(直到itemNum11)才能滚动到下一行。
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?
我想实现这一点,当我调用这个方法时,网格会将下一行带到网格的顶部(就像滚动条一样)。为什么它不起作用?
回答by Tsabo
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;
}
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]);
回答by Hannish
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! ;-)
希望它有帮助,它肯定对我有帮助!;-)

