Skip to content

Commit 9c288e9

Browse files
BornToBeRootclaude
andcommitted
Fix: IP Scanner columns can be resized beyond the window (#3557, prototype)
With a visible star column (Vendor, Width="*") the DataGrid only lets a column grow by taking the width from the columns to its right (down to their MinWidth, DataGridColumnCollection.RecomputeColumnWidthsOnColumnPositiveResize); the rest is dropped, so a column couldn't be made wider than the grid (also not with a double-click on the gripper) unless there was free space. New DataGridHelper.FillLastColumn: the last visible column fills the remaining width with a pixel width (max(base width, available width - other visible columns); base = MinWidth or the width the user resized it to), updated on size / viewport / column width / visibility changes, previous fill column restored when another one becomes the last. No star column -> columns grow freely, the grid scrolls horizontally. Used in the IP Scanner instead of Width="*" and RefreshStarColumnsOnFirstRow (not needed without a star column). Prototype - roll out to the other tools after testing on Windows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e6311b2 commit 9c288e9

3 files changed

Lines changed: 273 additions & 3 deletions

File tree

‎Source/NETworkManager.Utilities.WPF/DataGridHelper.cs‎

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.ComponentModel;
3+
using System.Linq;
24
using System.Windows;
35
using System.Windows.Controls;
6+
using System.Windows.Media;
47
using System.Windows.Threading;
58

69
namespace NETworkManager.Utilities.WPF;
@@ -57,4 +60,271 @@ void Handler(object sender, DataGridRowEventArgs args)
5760
}), DispatcherPriority.ContextIdle);
5861
}
5962
}
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
60330
}

‎Source/NETworkManager/Views/IPScannerView.xaml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@
258258
SelectedItem="{Binding Path=SelectedResult}"
259259
SelectedItemsList="{Binding Path=SelectedResults, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
260260
Sorting="DataGrid_OnSorting"
261-
wpfHelpers:DataGridHelper.RefreshStarColumnsOnFirstRow="True"
261+
wpfHelpers:DataGridHelper.FillLastColumn="True"
262262
RowDetailsVisibilityMode="{Binding Path=HostDetailsVisibilityMode}"
263263
EnableRowVirtualization="True"
264264
ScrollViewer.CanContentScroll="True"
@@ -760,8 +760,7 @@
760760
<DataGridTextColumn Header="{x:Static localization:Strings.Vendor}"
761761
Binding="{Binding Vendor, Converter={StaticResource StringNullOrEmptyToStringConverter}}"
762762
SortMemberPath="Vendor"
763-
MinWidth="200"
764-
Width="*">
763+
MinWidth="200">
765764
<DataGridTextColumn.Visibility>
766765
<MultiBinding Converter="{StaticResource BooleansOrToVisibilityCollapsedConverter}">
767766
<Binding Source="{StaticResource BindingProxy}"

‎Website/docs/changelog/next-release.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ Release date: **xx.xx.2026**
256256
- Fixed hostnames resolved via DNS (`PTR`) being shown with a trailing dot (e.g. `server.example.com.`), also in the export and when opened in another feature (e.g. **DNS Lookup**, **Remote Desktop**). [#PR](https://github.com/BornToBeRoot/NETworkManager/pull/PR)
257257
- Fixed a NetBIOS name (shown as hostname if there is no DNS hostname) being passed to other features (e.g. **Remote Desktop**, **Port Scanner**, **DNS Lookup**, **RDAP**), where it may not be resolvable via DNS. The DNS hostname is used, otherwise the IP address; **DNS lookup...** / **RDAP...** > **Hostname** need a DNS hostname. [#PR](https://github.com/BornToBeRoot/NETworkManager/pull/PR)
258258
- Fixed the progress and the up / down counters possibly staying below the final values (e.g. 255 of 256 hosts scanned) when the results of several hosts arrived at the same time. [#PR](https://github.com/BornToBeRoot/NETworkManager/pull/PR)
259+
- Fixed columns that couldn't be made wider (by dragging or double-clicking the gripper) if the window was not wide enough, e.g. to see a long hostname. The last column still fills the remaining width, other columns can now grow beyond the window with a horizontal scrollbar. [#3557](https://github.com/BornToBeRoot/NETworkManager/issues/3557)
259260

260261
**Port Scanner**
261262

0 commit comments

Comments
 (0)