WPF Toolkit:如何滚动数据网格以显示后面代码中的所选项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1984882/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:23:03  来源:igfitidea点击:

WPF Toolkit: how to scroll datagrid to show selected item from code behind?

wpfscrolldatagridselectionwpftoolkit

提问by Akash Kava

I tried the following, all of which fail on function ScrollIntoView and give a NullReferenceException:

我尝试了以下操作,所有这些都在函数 ScrollIntoView 上失败并给出 NullReferenceException:

// doesn't work
grid.SelectedItem = sItem;
grid.ScrollIntoView(sItem);

// doesn't work
grid.SelectedItem = sItem;
grid.Focus();
grid.CurrentColumn = grid.Columns[0];
grid.UpdateLayout();
grid.ScrollIntoView(sItem,grid.Columns[0]);

// doesn't work
grid.SelectedItem = sItem;
grid.UpdateLayout();
grid.ScrollIntoView(sItem);

The problem is, when I select a row from code-behind, selection is not visible - it's somewhere down the bottom. Unless the user scrolls they feels that selection has vanished. I need to scroll a DataGrid to the point that user can see the selection.

问题是,当我从代码隐藏中选择一行时,选择是不可见的——它在底部的某个地方。除非用户滚动,否则他们会觉得选择已经消失。我需要将 DataGrid 滚动到用户可以看到选择的点。

I also tried "BringIntoView" as well but no luck.

我也尝试过“BringIntoView”,但没有运气。

采纳答案by Akash Kava

Virtualized Stack Panel didn't have an item container, because Item Container does not exist for the item outside the view and that's why this error was shown. Disabling virtualization resolves the issue for now, and the bug has been reported to codeplex toolkit project.

Virtualized Stack Panel 没有项目容器,因为视图外的项目不存在项目容器,这就是显示此错误的原因。禁用虚拟化暂时解决了该问题,并且该错误已报告给 codeplex 工具包项目。

回答by Keith Harrison

Try:

尝试:

grid.SelectedItem = sItem; 
grid.UpdateLayout();
grid.ScrollIntoView(grid.SelectedItem);