using System.Globalization; using Avalonia.Controls; using Avalonia.Media; namespace Nonemm.App.Windows; /// Stops a grid column being dragged, or sized to its cells, narrower than its /// own heading. public static class ColumnWidths { /// What a header takes besides its text: its padding and the room the theme /// keeps for the sort arrow. private const double HeaderPadding = 40; /// The width of the heading in the grid's font, with the room the arrow /// needs. public static double ForHeader(DataGrid grid, string heading) => new FormattedText( heading, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(grid.FontFamily, FontStyle.Normal, FontWeight.Bold), grid.FontSize, null).Width + HeaderPadding; public static void KeepHeadersReadable(DataGrid grid) { foreach (DataGridColumn column in grid.Columns) { if (column.Header is string heading) { column.MinWidth = ForHeader(grid, heading); } } } }