Forcing AddNewRow Cell to Activate And EnterEditMode

Danny / Monday, October 19, 2009

Unfortunately this code took me the better part of a day to get right (and clean)  I figured I would share it with the community.  the GetDependencyObject method can obviously be used to traverse any dependency object.  Enjoy.

object CellControl = new object();
GetDependencyObject(OccurrenceGrid, typeof(AddNewRowSelectorCellControl), ref CellControl);
RowBase Base = (CellControl as AddNewRowSelectorCellControl).Cell.Row;
OccurrenceGrid.ActiveCell = Base.Cells[0];
OccurrenceGrid.EnterEditMode((Row)Base);

private void GetDependencyObject(DependencyObject depObj, Type searchType, ref object RetVal)
{
 for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
 {
  if (VisualTreeHelper.GetChild(depObj, i).GetType() != searchType)
  {
   GetDependencyObject(VisualTreeHelper.GetChild(depObj, i), searchType, ref RetVal);
  }
  else
  {
   RetVal = (object)VisualTreeHelper.GetChild(depObj, i);
  }
 }
}