public Rect GetCellBoundsFor(IUIElement uiElement, double xOffset, double yOffset) { int firstColumn = uiElement.GridColumn().Clamp(0, _columns.Length - 1); int columnSpan = uiElement.GridColumnSpan().Clamp(1, _columns.Length - firstColumn); int lastColumn = firstColumn + columnSpan; int firstRow = uiElement.GridRow().Clamp(0, _rows.Length - 1); int rowSpan = uiElement.GridRowSpan().Clamp(1, _rows.Length - firstRow); int lastRow = firstRow + rowSpan; double top = TopEdgeOfRow(firstRow); double left = LeftEdgeOfColumn(firstColumn); double width = 0; double height = 0; for (int n = firstColumn; n < lastColumn; n++) { width += _columns[n].Size; } for (int n = firstRow; n < lastRow; n++) { height += _rows[n].Size; } // Account for any space between spanned rows/columns width += (columnSpan - 1) * _columnSpacing; height += (rowSpan - 1) * _rowSpacing; return(new Rect(left + xOffset, top + yOffset, width, height)); }