|
1 | 1 | using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Linq; |
2 | 4 | using System.Windows; |
3 | 5 | using System.Windows.Controls; |
| 6 | +using System.Windows.Media; |
4 | 7 | using System.Windows.Threading; |
5 | 8 |
|
6 | 9 | namespace NETworkManager.Utilities.WPF; |
@@ -57,4 +60,271 @@ void Handler(object sender, DataGridRowEventArgs args) |
57 | 60 | }), DispatcherPriority.ContextIdle); |
58 | 61 | } |
59 | 62 | } |
| 63 | + |
| 64 | + #region FillLastColumn |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// When set to <see langword="true"/>, the last visible column fills the remaining width of the |
| 68 | + /// <see cref="DataGrid"/> - like a star (<c>*</c>) column, but with a pixel width. With a visible star column the |
| 69 | + /// DataGrid only lets a column grow by taking the width from the columns to its right (down to their |
| 70 | + /// <c>MinWidth</c>), so a column can't be resized wider than the grid (also not with a double-click on the |
| 71 | + /// gripper), there is never a horizontal scrollbar (#3557). Without star columns a column grows freely and the |
| 72 | + /// grid scrolls horizontally. |
| 73 | + /// <para> |
| 74 | + /// The fill column gets max(base width, available width - other visible columns). The base width is its |
| 75 | + /// <c>MinWidth</c>, or the width the user resized it to. It is updated when the size of the grid / its viewport |
| 76 | + /// changes and when another column is resized or shown / hidden. A column that is no longer the last visible one |
| 77 | + /// gets its original width back. Don't use star columns in the same grid. |
| 78 | + /// </para> |
| 79 | + /// </summary> |
| 80 | + public static readonly DependencyProperty FillLastColumnProperty = |
| 81 | + DependencyProperty.RegisterAttached( |
| 82 | + "FillLastColumn", |
| 83 | + typeof(bool), |
| 84 | + typeof(DataGridHelper), |
| 85 | + new PropertyMetadata(false, OnFillLastColumnChanged)); |
| 86 | + |
| 87 | + public static void SetFillLastColumn(DataGrid element, bool value) => |
| 88 | + element.SetValue(FillLastColumnProperty, value); |
| 89 | + |
| 90 | + public static bool GetFillLastColumn(DataGrid element) => |
| 91 | + (bool)element.GetValue(FillLastColumnProperty); |
| 92 | + |
| 93 | + // State of a grid with FillLastColumn (subscriptions, current fill column) |
| 94 | + private static readonly DependencyProperty FillStateProperty = |
| 95 | + DependencyProperty.RegisterAttached( |
| 96 | + "FillState", |
| 97 | + typeof(FillState), |
| 98 | + typeof(DataGridHelper), |
| 99 | + new PropertyMetadata(null)); |
| 100 | + |
| 101 | + private static readonly DependencyPropertyDescriptor ColumnActualWidthDescriptor = |
| 102 | + DependencyPropertyDescriptor.FromProperty(DataGridColumn.ActualWidthProperty, typeof(DataGridColumn)); |
| 103 | + |
| 104 | + private static readonly DependencyPropertyDescriptor ColumnVisibilityDescriptor = |
| 105 | + DependencyPropertyDescriptor.FromProperty(DataGridColumn.VisibilityProperty, typeof(DataGridColumn)); |
| 106 | + |
| 107 | + private static void OnFillLastColumnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 108 | + { |
| 109 | + if (d is not DataGrid dataGrid) |
| 110 | + return; |
| 111 | + |
| 112 | + if (e.NewValue is true) |
| 113 | + { |
| 114 | + var state = new FillState(dataGrid); |
| 115 | + |
| 116 | + dataGrid.SetValue(FillStateProperty, state); |
| 117 | + |
| 118 | + dataGrid.Loaded += state.OnLoaded; |
| 119 | + dataGrid.Unloaded += state.OnUnloaded; |
| 120 | + |
| 121 | + if (dataGrid.IsLoaded) |
| 122 | + state.Attach(); |
| 123 | + } |
| 124 | + else if (dataGrid.GetValue(FillStateProperty) is FillState state) |
| 125 | + { |
| 126 | + dataGrid.Loaded -= state.OnLoaded; |
| 127 | + dataGrid.Unloaded -= state.OnUnloaded; |
| 128 | + |
| 129 | + state.Detach(); |
| 130 | + |
| 131 | + dataGrid.ClearValue(FillStateProperty); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Subscriptions and the current fill column of a grid. The column change notifications |
| 137 | + /// (<see cref="DependencyPropertyDescriptor.AddValueChanged"/>) hold strong references, so they are only |
| 138 | + /// subscribed while the grid is loaded. |
| 139 | + /// </summary> |
| 140 | + private sealed class FillState(DataGrid dataGrid) |
| 141 | + { |
| 142 | + private ScrollViewer _scrollViewer; |
| 143 | + private DataGridColumn[] _columns = []; |
| 144 | + |
| 145 | + // Current fill column, its original width (restored when another column fills) and base width |
| 146 | + private DataGridColumn _fillColumn; |
| 147 | + private DataGridLength _fillColumnOriginalWidth; |
| 148 | + private double _fillColumnBaseWidth; |
| 149 | + |
| 150 | + // Width set by Update - another width of the fill column was set by the user (resize / double-click) |
| 151 | + private double _lastSetWidth = double.NaN; |
| 152 | + private bool _isUpdating; |
| 153 | + private bool _isUpdateQueued; |
| 154 | + |
| 155 | + public void OnLoaded(object sender, RoutedEventArgs e) => Attach(); |
| 156 | + |
| 157 | + public void OnUnloaded(object sender, RoutedEventArgs e) => Detach(); |
| 158 | + |
| 159 | + public void Attach() |
| 160 | + { |
| 161 | + Detach(); |
| 162 | + |
| 163 | + _columns = [.. dataGrid.Columns]; |
| 164 | + |
| 165 | + foreach (var column in _columns) |
| 166 | + { |
| 167 | + ColumnActualWidthDescriptor.AddValueChanged(column, OnColumnChanged); |
| 168 | + ColumnVisibilityDescriptor.AddValueChanged(column, OnColumnChanged); |
| 169 | + } |
| 170 | + |
| 171 | + dataGrid.SizeChanged += OnSizeChanged; |
| 172 | + |
| 173 | + _scrollViewer = FindScrollViewer(dataGrid); |
| 174 | + |
| 175 | + if (_scrollViewer != null) |
| 176 | + _scrollViewer.ScrollChanged += OnScrollChanged; |
| 177 | + |
| 178 | + QueueUpdate(); |
| 179 | + } |
| 180 | + |
| 181 | + public void Detach() |
| 182 | + { |
| 183 | + foreach (var column in _columns) |
| 184 | + { |
| 185 | + ColumnActualWidthDescriptor.RemoveValueChanged(column, OnColumnChanged); |
| 186 | + ColumnVisibilityDescriptor.RemoveValueChanged(column, OnColumnChanged); |
| 187 | + } |
| 188 | + |
| 189 | + _columns = []; |
| 190 | + |
| 191 | + dataGrid.SizeChanged -= OnSizeChanged; |
| 192 | + |
| 193 | + if (_scrollViewer != null) |
| 194 | + _scrollViewer.ScrollChanged -= OnScrollChanged; |
| 195 | + |
| 196 | + _scrollViewer = null; |
| 197 | + } |
| 198 | + |
| 199 | + private void OnSizeChanged(object sender, SizeChangedEventArgs e) => QueueUpdate(); |
| 200 | + |
| 201 | + private void OnScrollChanged(object sender, ScrollChangedEventArgs e) |
| 202 | + { |
| 203 | + // Viewport width changes e.g. when the vertical scrollbar appears; extent changes are caused by the |
| 204 | + // columns (handled via OnColumnChanged) |
| 205 | + if (!DoubleUtilIsZero(e.ViewportWidthChange)) |
| 206 | + QueueUpdate(); |
| 207 | + } |
| 208 | + |
| 209 | + private void OnColumnChanged(object sender, EventArgs e) |
| 210 | + { |
| 211 | + if (_isUpdating) |
| 212 | + return; |
| 213 | + |
| 214 | + // The user resized the fill column (or double-clicked its gripper): that is its new base width |
| 215 | + if (ReferenceEquals(sender, _fillColumn) && !double.IsNaN(_lastSetWidth) && |
| 216 | + Math.Abs(_fillColumn.ActualWidth - _lastSetWidth) > 0.5) |
| 217 | + _fillColumnBaseWidth = _fillColumn.ActualWidth; |
| 218 | + |
| 219 | + QueueUpdate(); |
| 220 | + } |
| 221 | + |
| 222 | + // Several changes in one layout pass (e.g. all columns while rows are added) -> one update |
| 223 | + private void QueueUpdate() |
| 224 | + { |
| 225 | + if (_isUpdateQueued) |
| 226 | + return; |
| 227 | + |
| 228 | + _isUpdateQueued = true; |
| 229 | + |
| 230 | + dataGrid.Dispatcher.BeginInvoke(new Action(() => |
| 231 | + { |
| 232 | + _isUpdateQueued = false; |
| 233 | + Update(); |
| 234 | + }), DispatcherPriority.Loaded); |
| 235 | + } |
| 236 | + |
| 237 | + private void Update() |
| 238 | + { |
| 239 | + if (!dataGrid.IsLoaded) |
| 240 | + return; |
| 241 | + |
| 242 | + var lastVisibleColumn = dataGrid.Columns |
| 243 | + .Where(x => x.Visibility == Visibility.Visible) |
| 244 | + .OrderBy(x => x.DisplayIndex) |
| 245 | + .LastOrDefault(); |
| 246 | + |
| 247 | + _isUpdating = true; |
| 248 | + |
| 249 | + try |
| 250 | + { |
| 251 | + if (!ReferenceEquals(lastVisibleColumn, _fillColumn)) |
| 252 | + { |
| 253 | + // Give the previous fill column its original width back |
| 254 | + if (_fillColumn != null && dataGrid.Columns.Contains(_fillColumn)) |
| 255 | + _fillColumn.Width = _fillColumnOriginalWidth; |
| 256 | + |
| 257 | + _fillColumn = lastVisibleColumn; |
| 258 | + _lastSetWidth = double.NaN; |
| 259 | + |
| 260 | + if (_fillColumn == null) |
| 261 | + return; |
| 262 | + |
| 263 | + _fillColumnOriginalWidth = _fillColumn.Width; |
| 264 | + _fillColumnBaseWidth = _fillColumn.MinWidth; |
| 265 | + } |
| 266 | + |
| 267 | + if (_fillColumn == null) |
| 268 | + return; |
| 269 | + |
| 270 | + var availableWidth = GetAvailableWidth(); |
| 271 | + |
| 272 | + if (availableWidth <= 0) |
| 273 | + return; |
| 274 | + |
| 275 | + var otherColumnsWidth = dataGrid.Columns |
| 276 | + .Where(x => x.Visibility == Visibility.Visible && !ReferenceEquals(x, _fillColumn)) |
| 277 | + .Sum(x => x.ActualWidth); |
| 278 | + |
| 279 | + // Floor - a fraction of a pixel too much would show the horizontal scrollbar |
| 280 | + var width = Math.Max(_fillColumnBaseWidth, Math.Floor(availableWidth - otherColumnsWidth)); |
| 281 | + |
| 282 | + width = Math.Min(Math.Max(width, _fillColumn.MinWidth), _fillColumn.MaxWidth); |
| 283 | + |
| 284 | + if (!double.IsNaN(_lastSetWidth) && Math.Abs(width - _lastSetWidth) < 0.5 && |
| 285 | + Math.Abs(_fillColumn.ActualWidth - width) < 0.5) |
| 286 | + return; |
| 287 | + |
| 288 | + _lastSetWidth = width; |
| 289 | + _fillColumn.Width = new DataGridLength(width); |
| 290 | + } |
| 291 | + finally |
| 292 | + { |
| 293 | + _isUpdating = false; |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + /// <summary> |
| 298 | + /// Gets the width available for the columns: the viewport of the grid's scroll viewer (without the vertical |
| 299 | + /// scrollbar) minus the row headers. |
| 300 | + /// </summary> |
| 301 | + private double GetAvailableWidth() |
| 302 | + { |
| 303 | + var width = _scrollViewer is { ViewportWidth: > 0 } ? _scrollViewer.ViewportWidth : dataGrid.ActualWidth; |
| 304 | + |
| 305 | + return width - dataGrid.CellsPanelHorizontalOffset; |
| 306 | + } |
| 307 | + |
| 308 | + private static bool DoubleUtilIsZero(double value) => Math.Abs(value) < 0.01; |
| 309 | + |
| 310 | + private static ScrollViewer FindScrollViewer(DependencyObject parent) |
| 311 | + { |
| 312 | + for (var i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) |
| 313 | + { |
| 314 | + var child = VisualTreeHelper.GetChild(parent, i); |
| 315 | + |
| 316 | + if (child is ScrollViewer scrollViewer) |
| 317 | + return scrollViewer; |
| 318 | + |
| 319 | + var result = FindScrollViewer(child); |
| 320 | + |
| 321 | + if (result != null) |
| 322 | + return result; |
| 323 | + } |
| 324 | + |
| 325 | + return null; |
| 326 | + } |
| 327 | + } |
| 328 | + |
| 329 | + #endregion |
60 | 330 | } |
0 commit comments