DataGrid在ScrollView中保留所选项 [英] DataGrid Keep Selected Item in the ScrollView

查看:94
本文介绍了DataGrid在ScrollView中保留所选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF DataGrid,它位于Grid内部并且有很多行,因此有滚动条。默认情况下,当用户滚动DataGrid并且SelectedItem可以退出视图时。当发生这种情况时,我希望SelectedItem更改并成为datagrid视图区域中的第一个项目,因此当用户搜索DataGrid时,SelectedItem始终处于视图中。我已经使用WindowsForms和DataGridView完成了这项工作,但是还没有弄清楚如何在WPF中使用DataGrid来实现这一点。关于最佳方法的任何想法?我找到了DataGrid的ScrollViewer.ScrollChanged事件,它似乎是一个很好的起点,不知道从哪里开始。我正在使用VB,但C#代码会很好,我可以翻译。

I have a WPF DataGrid that is inside of a Grid and has many rows, and thus has scroll bars. By default, when the user scrolls the DataGrid and the SelectedItem can sroll out of view. When this happens I would like the SelectedItem to change and become the first item that is in the datagrid view area so a SelectedItem is alway in view as the user scolls the DataGrid. I have done this in with WindowsForms and a DataGridView but have not figured out how to do this using a DataGrid in WPF. Any ideas as to the best approach for this? I found the ScrollViewer.ScrollChanged event for the DataGrid which seems like a good place to start, not sure where to go from there. I am using VB but C# code would be good to, I can translate.

推荐答案

您可以使用方法 System.Windows.Controls。 Data.DataGrid.ScrollIntoView

http://msdn.microsoft.com/en-us/library/cc673083%28v=vs.95%29.aspx [ ^ ]。



当然,不要从 ScrollChanged 事件处理程序中调用此方法,以避免无限递归。只有当您添加或删除项目或用户请求时才能调用它。



-SA
You can use the method System.Windows.Controls.Data.DataGrid.ScrollIntoView:
http://msdn.microsoft.com/en-us/library/cc673083%28v=vs.95%29.aspx[^].

Of course, don''t call this method from ScrollChanged event handler, to avoid "infinite" recursion. Call it only when you add or remove items, or by user''s request.

—SA

这篇关于DataGrid在ScrollView中保留所选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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