public static GridBox GetComputedGridBoxContainingRect(Rect rect, GridElement grid) { int columnBeforePosition = grid.GetComputedColumnBeforePosition(rect.Left); int columnAfterPosition = grid.GetComputedColumnAfterPosition(rect.Right); int rowBeforePosition = grid.GetComputedRowBeforePosition(rect.Top); int rowAfterPosition = grid.GetComputedRowAfterPosition(rect.Bottom); if (columnAfterPosition > columnBeforePosition + 1 && rect.Right < grid.GetComputedPositionOfColumn(columnAfterPosition - 1) + grid.GetComputedColumnWidth(columnAfterPosition - 1) / 2.0) { --columnAfterPosition; } if (columnAfterPosition > columnBeforePosition + 1 && rect.Left > grid.GetComputedPositionOfColumn(columnBeforePosition) + grid.GetComputedColumnWidth(columnBeforePosition) / 2.0) { ++columnBeforePosition; } if (columnAfterPosition == columnBeforePosition + 1 && columnAfterPosition < grid.ColumnDefinitions.Count && grid.GetComputedPositionOfColumn(columnAfterPosition) < (rect.Left + rect.Right) / 2.0) { ++columnBeforePosition; ++columnAfterPosition; } if (rowAfterPosition > rowBeforePosition + 1 && rect.Bottom < grid.GetComputedPositionOfRow(rowAfterPosition - 1) + grid.GetComputedRowHeight(rowAfterPosition - 1) / 2.0) { --rowAfterPosition; } if (rowAfterPosition > rowBeforePosition + 1 && rect.Top > grid.GetComputedPositionOfRow(rowBeforePosition) + grid.GetComputedRowHeight(rowBeforePosition) / 2.0) { ++rowBeforePosition; } if (rowAfterPosition == rowBeforePosition + 1 && rowAfterPosition < grid.RowDefinitions.Count && grid.GetComputedPositionOfRow(rowAfterPosition) < (rect.Top + rect.Bottom) / 2.0) { ++rowBeforePosition; ++rowAfterPosition; } return(new GridBox(columnBeforePosition, columnAfterPosition, rowBeforePosition, rowAfterPosition)); }