没有在UserControl WPF中设置焦点 [英] Focus is not set in UserControl WPF
问题描述
我无法将重点放在用户控制项上
我在用户控件中使用DataGrid
我想把重点放在DataGrid的第一行
我发现以下代码
I cannot able to set focus on User Control Item
I am using DataGrid in User Control
I want to set focus on First row of DataGrid
I found following code
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/zh-CN/wpf/thread/ab95dd62-995f-481a-a765-d5efff1d559c/ [
By running above code I found ContainerFromIndex method always returns null
To rid of null problem I found following link
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ab95dd62-995f-481a-a765-d5efff1d559c/[^]
I come out of null problem using above link but still focus is not set on data grid
Thanks for reading my question
推荐答案
似乎您也需要给予控件焦点.这是在WPF中设置焦点 [ ^ ].
It seems that you will need to give the control focus as well. here is a link to a blog post on setting focus in WPF[^].
这篇关于没有在UserControl WPF中设置焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!