Click here to Skip to main content
15,741,818 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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];
                    DataGridRow dgrow = (DataGridRow)dgAccountInfo.ItemContainerGenerator.ContainerFromIndex(index);
      if (dgrow != null)
               dgAccountInfo.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

By running above code I found ContainerFromIndex method always returns null

To rid of null problem I found following link[^]

I come out of null problem using above link but still focus is not set on data grid

Thanks for reading my question

1 solution

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[^].
Share this answer
Khaniya 14-Feb-11 0:11am    
thanks for your replay
but does not work for me
My issue is, as mention in subject, to set focus within user control

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900