How to iterate over a DataGrid's cell controls for unit tests?

Oct 14, 2008 at 11:02 AM
One thing we do in our unit tests is to set values on the controls (e.g. set the Text property of TextBoxes), and see that the values go through the data bindings, are converted as expected by value converters, and perhaps stopped by validators.

I found a way to find all DataCellItems and iterate over them on this forum, ie.
  IList<DataGridCellInfo> cells = tupleGrid.SelectedCells;

But since the DataGridCellInfo.Item refers to the original source property list member corresponding to the cell, setting values there doesn't really test any thing.  At least it doesn't test the stuff I'd like to test, which is validators and custom value converters on the data binding.

Is there a way to get at the edit controls of the cells?

Oct 14, 2008 at 11:28 AM
I found a way, I think.

   IList<DataGridCellInfo> cells = tupleGrid.SelectedCells;
   DataGridColumn firstColumn = tupleGrid.Columns[0];
and then in the iteration over cells:
   for (int i = 0; i < op.AListOfStucts.Count; ++i)
      SillyClassWithOneField cellItem = (SillyClassWithOneField) cells[i].Item;
      TextBlock cellControl = (TextBlock) firstColumn.GetCellContent(cellItem);
      Assert.AreEqual(op.AListOfStucts[i].AStringValueThatBecomesTheColumnHeader, cellControl.Text, "The cell item of each row in a data grid, should be the same as the matching item in the list property");

(I can drop that cellItem variable now, I guess...?)