Skip to content

Commit 9d0a72c

Browse files
committed
implemented auto resizing column by double clicking on resize area
1 parent 223d8a6 commit 9d0a72c

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/WinUI.TableView/TableViewCell.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using CommunityToolkit.WinUI;
2-
using Microsoft.UI.Input;
1+
using Microsoft.UI.Input;
32
using Microsoft.UI.Xaml;
43
using Microsoft.UI.Xaml.Controls;
54
using Microsoft.UI.Xaml.Input;
65
using Microsoft.UI.Xaml.Media;
76
using System;
87
using System.Linq;
98
using System.Threading.Tasks;
9+
using Windows.Foundation;
1010
using Windows.System;
1111
using Windows.UI.Core;
1212

@@ -22,6 +22,13 @@ public TableViewCell()
2222
DefaultStyleKey = typeof(TableViewCell);
2323
}
2424

25+
protected override Size MeasureOverride(Size availableSize)
26+
{
27+
var size = base.MeasureOverride(availableSize);
28+
Column.DesiredWidth = Math.Max(Column.DesiredWidth, size.Width);
29+
return size;
30+
}
31+
2532
protected override void OnDoubleTapped(DoubleTappedRoutedEventArgs e)
2633
{
2734
if (!IsReadOnly)

src/WinUI.TableView/TableViewColumn.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ private void EnsureHeaderStyle()
7272
}
7373
}
7474

75+
internal double DesiredWidth { get; set; }
76+
7577
public static readonly DependencyProperty HeaderStyleProperty = DependencyProperty.Register(nameof(HeaderStyle), typeof(Style), typeof(TableViewColumn), new PropertyMetadata(null, (d, _) => ((TableViewColumn)d).EnsureHeaderStyle()));
7678
public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(nameof(Header), typeof(object), typeof(TableViewColumn), new PropertyMetadata(null));
7779
public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(nameof(Width), typeof(double), typeof(TableViewColumn), new PropertyMetadata(200d));

src/WinUI.TableView/TableViewColumnHeader.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,29 @@ private bool IsCursorInLeftResizeArea(PointerRoutedEventArgs args)
307307
return point.Position.X <= resizeArea && point.Position.Y < ActualHeight;
308308
}
309309

310+
protected override void OnDoubleTapped(DoubleTappedRoutedEventArgs e)
311+
{
312+
base.OnDoubleTapped(e);
313+
314+
if (IsSizingCursor && Column is not null)
315+
{
316+
var position = e.GetPosition(this);
317+
318+
if (position.X <= 8 && _headerRow?.GetPreviousHeader(this) is { Column: { } } header)
319+
{
320+
var width = Math.Clamp(header.Column.DesiredWidth, header.MinWidth, header.MaxWidth);
321+
header.Width = width;
322+
header.Measure(new Size(Width, ActualHeight));
323+
}
324+
else
325+
{
326+
var width = Math.Clamp(Column.DesiredWidth, MinWidth, MaxWidth);
327+
Width = width;
328+
Measure(new Size(Width, ActualHeight));
329+
}
330+
}
331+
}
332+
310333
protected override void OnPointerMoved(PointerRoutedEventArgs e)
311334
{
312335
base.OnPointerMoved(e);

0 commit comments

Comments
 (0)