WPF 中单击按钮上的数据网格行选择
我有一个数据网格,其中填充了数据表中的值。在我的程序中,我有四个按钮:转到第一个、最后一个、下一个和上一个,正如名称所示,我必须根据使用这些按钮所做的选择来选择行。如果我使用下面的代码来获取行(例如第一行),一切看起来都很好。
DataGridRow row =(DataGridRow)userControl.m_DataGrid.ItemContainerGenerator.ContainerFromIndex(0);
row.IsSelected = true;
但是当行数多于数据网格的高度时(当滚动条进入图片时),代码会抛出空值。
请帮助我解决这个问题。我认为这是因为视图问题。
I am having a Datagrid that gets populated with values from a DataTable. In my program i have four buttons: Goto First, Last, Next and Previous, as the name name indicates i have to select the rows based on the selection made using these buttons. Everything seems well if i use the below code to get the row (for example first row).
DataGridRow row =(DataGridRow)userControl.m_DataGrid.ItemContainerGenerator.ContainerFromIndex(0);
row.IsSelected = true;
But the code throws null value when there is more rows than the height of the Datagrid(When scrollbar comes into picture).
Please help me out of this issue. I think this is because of the view problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于虚拟化,容器仅在对象处于视图中时创建,因此您可以首先使用 相应方法,等待容器创建然后选择它。
由于这相当混乱,我建议使用
DataGridRow
样式将IsSelected
绑定到项目上的属性(将其设置为ItemContainerStyle
)。然后,您可以将该属性设置为 true 并将项目滚动到视图中(如果需要)。Due to virtualization the containers are only created when the object is in view, so you could first scroll the item into view using the respective method, wait for the creation of the container and then select it.
As this is rather messy i would suggest binding the
IsSelected
to a property on your item using a style forDataGridRow
(set it asItemContainerStyle
). Then you can just set the property to true and scroll the item into view if need be.