【发布时间】:2011-06-25 13:54:01
【问题描述】:
我无法将焦点设置在用户控制项上 我在用户控件中使用 DataGrid 我想将焦点放在 DataGrid 的第一行
我找到了以下代码
int index = 0;
dgAccountInfo.SelectedItem = winAccountInfoGrid.dgAccountInfo.Items[index];
dgAccountInfo.ScrollIntoView(winAccountInfoGrid.dgAccountInfo.Items[index]);
DataGridRow dgrow = (DataGridRow)dgAccountInfo.ItemContainerGenerator.ContainerFromIndex(index);
if (dgrow != null)
{
dgAccountInfo.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next))
};
通过运行上面的代码,我发现 ContainerFromIndex 方法总是返回 null
为了摆脱空问题,我发现以下链接
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ab95dd62-995f-481a-a765-d5efff1d559c/
我使用上面的链接解决了空问题,但仍然没有在数据网格上设置焦点
感谢您阅读我的问题
【问题讨论】: