|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: CommonControls.Images |
| 4 | + * FILE: ExtendedGrid.cs |
| 5 | + * PURPOSE: Extension for Grid Control, not elegant but does the job |
| 6 | + * PROGRAMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +// ReSharper disable MemberCanBeInternal |
| 10 | +// ReSharper disable MemberCanBePrivate.Global |
| 11 | +// ReSharper disable ArrangeBraces_for |
| 12 | +// ReSharper disable UnusedMember.Global, basic Dummy for later use |
| 13 | + |
| 14 | +using System.Windows; |
| 15 | +using System.Windows.Controls; |
| 16 | +using System.Windows.Media; |
| 17 | + |
| 18 | +namespace CommonControls.Images |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// The extended grid class. |
| 22 | + /// </summary> |
| 23 | + internal static class ExtendedGrid |
| 24 | + { |
| 25 | + /// <summary> |
| 26 | + /// The height and width of each cell. Min is 1. |
| 27 | + /// </summary> |
| 28 | + private static int _cellHeight = 1; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// The cell width |
| 32 | + /// </summary> |
| 33 | + private static int _cellWidth = 1; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets the number of columns. |
| 37 | + /// </summary> |
| 38 | + /// <value> |
| 39 | + /// The columns. |
| 40 | + /// </value> |
| 41 | + public static int Columns { get; private set; } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets the number of rows. |
| 45 | + /// </summary> |
| 46 | + public static int Rows { get; private set; } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Gets or sets the default cell size in pixels. |
| 50 | + /// </summary> |
| 51 | + public static int CellSize { get; set; } = 100; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Generates a grid with uniform cell size. |
| 55 | + /// </summary> |
| 56 | + /// <param name="columns">Number of columns in the grid.</param> |
| 57 | + /// <param name="rows">Number of rows in the grid.</param> |
| 58 | + /// <param name="gridLines">Indicates whether grid lines should be shown.</param> |
| 59 | + /// <returns>A <see cref="Grid" /> with uniform cell dimensions.</returns> |
| 60 | + public static Grid ExtendGrid(int columns, int rows, bool gridLines) |
| 61 | + { |
| 62 | + ValidateParameters(columns, rows, CellSize); |
| 63 | + |
| 64 | + Columns = columns; |
| 65 | + Rows = rows; |
| 66 | + _cellHeight = _cellWidth = CellSize; |
| 67 | + |
| 68 | + return InitializeGridBase(gridLines, _cellWidth * Columns, _cellHeight * Rows, null, null); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Generates a grid with specified cell dimensions. |
| 73 | + /// </summary> |
| 74 | + /// <param name="columns">Number of columns in the grid.</param> |
| 75 | + /// <param name="rows">Number of rows in the grid.</param> |
| 76 | + /// <param name="width">Cell width in pixels.</param> |
| 77 | + /// <param name="height">Cell height in pixels.</param> |
| 78 | + /// <param name="gridLines">Indicates whether grid lines should be shown.</param> |
| 79 | + /// <returns>A <see cref="Grid" /> with custom cell dimensions.</returns> |
| 80 | + public static Grid ExtendGrid(int columns, int rows, int width, int height, bool gridLines) |
| 81 | + { |
| 82 | + ValidateParameters(columns, rows, width, height); |
| 83 | + |
| 84 | + Columns = columns; |
| 85 | + Rows = rows; |
| 86 | + _cellWidth = width; |
| 87 | + _cellHeight = height; |
| 88 | + |
| 89 | + return InitializeGridBase(gridLines, _cellWidth * Columns, _cellHeight * Rows, null, null); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Initializes the grid with the base settings and dimensions. |
| 94 | + /// </summary> |
| 95 | + /// <param name="gridLines">Indicates whether grid lines should be shown.</param> |
| 96 | + /// <param name="width">Total grid width in pixels.</param> |
| 97 | + /// <param name="height">Total grid height in pixels.</param> |
| 98 | + /// <param name="columnWidths">Custom column widths if any, null otherwise.</param> |
| 99 | + /// <param name="rowHeights">Custom row heights if any, null otherwise.</param> |
| 100 | + /// <returns>A <see cref="Grid" /> configured with the specified parameters.</returns> |
| 101 | + private static Grid InitializeGridBase(bool gridLines, int width, int height, |
| 102 | + IReadOnlyCollection<int> columnWidths, IReadOnlyCollection<int> rowHeights) |
| 103 | + { |
| 104 | + var dynamicGrid = new Grid |
| 105 | + { |
| 106 | + HorizontalAlignment = HorizontalAlignment.Left, |
| 107 | + VerticalAlignment = VerticalAlignment.Top, |
| 108 | + ShowGridLines = gridLines, |
| 109 | + Width = width, |
| 110 | + Height = height, |
| 111 | +#if DEBUG |
| 112 | + Background = Brushes.Gray |
| 113 | +#endif |
| 114 | + }; |
| 115 | + |
| 116 | + if (columnWidths != null) |
| 117 | + { |
| 118 | + foreach (var colWidth in columnWidths) |
| 119 | + { |
| 120 | + dynamicGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(colWidth) }); |
| 121 | + } |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + for (var i = 0; i < Columns; i++) |
| 126 | + { |
| 127 | + dynamicGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(_cellWidth) }); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (rowHeights != null) |
| 132 | + { |
| 133 | + foreach (var rowHeight in rowHeights) |
| 134 | + { |
| 135 | + dynamicGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(rowHeight) }); |
| 136 | + } |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + for (var i = 0; i < Rows; i++) |
| 141 | + { |
| 142 | + dynamicGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(_cellHeight) }); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return dynamicGrid; |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Validates the grid parameters to ensure they are non-negative. |
| 151 | + /// </summary> |
| 152 | + private static void ValidateParameters(int columns, int rows, int width = 1, int height = 1) |
| 153 | + { |
| 154 | + if (columns < 0 || rows < 0 || width < 1 || height < 1) |
| 155 | + { |
| 156 | + throw new Exception(ComCtlResources.GridExceptionValidate); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments