Getting values from Infragistics WPF grid

Feb 18, 2009 at 3:45 AM
The implementations of ListView and Table do not support the Infragistics WPF grid.  What I found is that the finder.Descendants() does not identify the griditems as children - this seems to be related to the way that the Microsoft UIA is actually calling into the grid.  To resolve this issue, I found that using the GridPattern method of getting child rows and cells worked.  I plan to wrap it into a GridView class with my own mapping from ControlType.DataView, to this type.

Here's some sample code which demonstrates getting cells and values:

public virtual bool GridContainsItem(string itemToCheck)
        {
            // here listItems is a reference to a ListView.  The ListView gets set properly, but the Rows.Count was always 0.
            GridPattern grid = this.listItems.AutomationElement.GetCurrentPattern(GridPattern.Pattern) as GridPattern;
            int columnCount = grid.Current.ColumnCount;
            int rowCount = grid.Current.RowCount;
            if (rowCount == 0)
                return false;

            for (int nCurrentRow = 0; nCurrentRow < rowCount; nCurrentRow++)
            {
                // get the value of row n, column 0
                AutomationElement cell = grid.GetItem(nCurrentRow, 0);

                ValuePattern valuePattern = cell.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
                string value = valuePattern.Current.Value;
                if (value == itemToCheck)
                    return true;
            }

            return false;
        }