c# - Set focus inside DataGrid -


this question looks simple, cant find answer that. have datagrid data loaded. need set focus in first row of grid.

i call method button onclick event.

the mygrid.focus() not focuses rows inside grid.

it 1 of hardest job in wpf, , reason because of virtualization , fact ui render thread different thread runs our code (you can not find when ui render finished). complete reference can have here http://social.technet.microsoft.com/wiki/contents/articles/21202.wpf-programmatically-selecting-and-focusing-a-row-or-cell-in-a-datagrid.aspx

using dispatcher.invoke might works situations (believe me in wpf dispatcher.invoke best friend , biggest enemy)

dggrid.itemssource = new list<object>() { new { = 10, j = 20 }, new { = 10, j = 20 }, new { = 10, j = 20 }, new { = 10, j = 20 }, new { = 10, j = 20 } };         dispatcher.invoke(new action(delegate()         {             grd.selectedindex = 0;             grd.focus();         }     ), system.windows.threading.dispatcherpriority.background); 

or robustest 1 linked article

public static datagridcell getcell(datagrid datagrid, datagridrow rowcontainer, int column) {     if (rowcontainer != null)     {         datagridcellspresenter presenter = findvisualchild<datagridcellspresenter>(rowcontainer);         if (presenter == null)         {             /* if row has been virtualized away, call applytemplate() method              * build visual tree in order datagridcellspresenter              * , datagridcells created */             rowcontainer.applytemplate();             presenter = findvisualchild<datagridcellspresenter>(rowcontainer);         }         if (presenter != null)         {             datagridcell cell = presenter.itemcontainergenerator.containerfromindex(column) datagridcell;             if (cell == null)             {                 /* bring column view                  * in case has been virtualized away */                 datagrid.scrollintoview(rowcontainer, datagrid.columns[column]);                 cell = presenter.itemcontainergenerator.containerfromindex(column) datagridcell;             }             return cell;         }     }     return null; }    public static void selectrowbyindex(datagrid datagrid, int rowindex)     {         if (!datagrid.selectionunit.equals(datagridselectionunit.fullrow))             throw new argumentexception("the selectionunit of datagrid must set fullrow.");          if (rowindex < 0 || rowindex > (datagrid.items.count - 1))             throw new argumentexception(string.format("{0} invalid row index.", rowindex));          datagrid.selecteditems.clear();         /* set selecteditem property */         object item = datagrid.items[rowindex]; // = product x         datagrid.selecteditem = item;          datagridrow row = datagrid.itemcontainergenerator.containerfromindex(rowindex) datagridrow;         if (row == null)         {             /* bring data item (product object) view              * in case has been virtualized away */             datagrid.scrollintoview(item);             row = datagrid.itemcontainergenerator.containerfromindex(rowindex) datagridrow;         }          if (row != null)     {         datagridcell cell = getcell(datagrid, row, 0);         if(cell != null)             cell.focus();     }  } 

you need add these static methods call second 1 first tries tries find (or draw row if draw yet set focus on it).


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -