c# - Focus is not set in UserControl WPF -


i cannot able set focus on user control item using datagrid in user control want 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)) }; 

by running above code found containerfromindex method returns null

to rid of null problem 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 still focus not set on data grid

thanks reading question

it appears that, in code you're using, data binding else, unnecessary if wanting set focus first item. if intention set focus (selected index) of datagrid, can accomplished in 1 single block of code.

if (dgaccountinfo.hasitems){       dgaccountinfo.selectedindex = 0; } 

the if construct ensures datagrid has items in (otherwise, next line of code throw exception). if datagrid has items, executes code inside construct. if datagrid has no items, skips right on code.

even if never plan have no items in datagrid, should include if construct anyway, in off chance happen

inside if construct, set datagrid's selectedindex first item. remember that, list box, datagrid "zero-based," meaning first item has index of zero. select nothing in datagrid, set selected index "-1"

in end, setting "selectedindex" far more reliable setting "selecteditem," items in datagrid change.

if wanted, include #else statement in above code (just above endif) if wanted else if datagrid has no items.

i hope helps!


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -