SharpCell.Grid

Painting in other UI frameworks

SharpCell.Grid holds everything about the grid that is not Avalonia or WPF: sizes and positions, hit testing, selection, copying, scrolling and painting. The two SpreadsheetView controls are thin hosts over it, and a host for another UI framework is the same few calls.

var painter = new GridPainter
{
    // Text width in your UI framework's font engine, for fitting numbers and running text.
    MeasureText = (text, font) => text.Length * font.Size * 0.5,
};
painter.Layout = SheetLayout.FromWorksheet(sheet, painter.MaxDigitWidth(sheet));
painter.FrozenRows = sheet.FrozenRows;
painter.FrozenColumns = sheet.FrozenColumns;

var viewport = new GridViewport(OffsetX: 0, OffsetY: 0, Width: 800, Height: 600);
var selection = GridSelection.Cell(new CellPosition(1, 1), sheet);
painter.Paint(canvas, sheet, viewport, selection);   // canvas: your IGridCanvas

// A click, then a drag to (300, 90): what Excel selects, and what is copied.
if (painter.Hit(viewport, x: 120, y: 30, sheet) is { } hit)
    selection = GridSelection.At(hit, sheet).ExtendTo(painter.CellNearest(viewport, 300, 90), sheet);
string copied = GridClipboard.Text(sheet, selection.Area);

The parts

Measuring text

GridPainter.MeasureText gives the width of text in a font, from the host's font engine. With it, numbers are fitted to their column, text runs on over empty cells, and row headers widen for long row numbers. Without it, nothing is fitted and Calibri 11's digit width (7 pixels) is assumed.

GridFont sizes are in device-independent pixels: 11 points are 14.67 pixels at 96 DPI. GridPainter.FontOf converts a SharpCell font.