diff --git a/src/ColumnAutoWidthMode.cs b/src/ColumnAutoWidthMode.cs
new file mode 100644
index 00000000..e036a089
--- /dev/null
+++ b/src/ColumnAutoWidthMode.cs
@@ -0,0 +1,22 @@
+namespace WinUI.TableView;
+
+///
+/// Specifies the behavior for automatic column width.
+///
+public enum ColumnAutoWidthMode
+{
+ ///
+ /// Column width is adjusted to both header and maximum cell width.
+ ///
+ Both,
+
+ ///
+ /// Column width is adjusted to maximum cell width.
+ ///
+ Cells,
+
+ ///
+ /// Column width is adjusted to header width.
+ ///
+ Header
+}
diff --git a/src/Columns/TableViewColumn.cs b/src/Columns/TableViewColumn.cs
index 48ae832a..70f50fd5 100644
--- a/src/Columns/TableViewColumn.cs
+++ b/src/Columns/TableViewColumn.cs
@@ -183,6 +183,15 @@ public double ActualWidth
set => SetValue(ActualWidthProperty, value);
}
+ ///
+ /// Gets or sets the ColumnAutoWidthMode of the column.
+ ///
+ public ColumnAutoWidthMode? ColumnAutoWidthMode
+ {
+ get => (ColumnAutoWidthMode?)GetValue(ColumnAutoWidthModeProperty);
+ set => SetValue(ColumnAutoWidthModeProperty, value);
+ }
+
///
/// Gets or sets a value indicating whether the column can be resized.
///
@@ -504,6 +513,11 @@ public string? SortMemberPath
///
public static readonly DependencyProperty ActualWidthProperty = DependencyProperty.Register(nameof(ActualWidth), typeof(double), typeof(TableViewColumn), new PropertyMetadata(0d, OnPropertyChanged));
+ ///
+ /// Identifies the ActualWidth dependency property.
+ ///
+ public static readonly DependencyProperty ColumnAutoWidthModeProperty = DependencyProperty.Register(nameof(ColumnAutoWidthMode), typeof(ColumnAutoWidthMode?), typeof(TableViewColumn), new PropertyMetadata(null));
+
///
/// Identifies the CanResize dependency property.
///
diff --git a/src/TableView.Properties.cs b/src/TableView.Properties.cs
index d90e7d26..c58c62d7 100644
--- a/src/TableView.Properties.cs
+++ b/src/TableView.Properties.cs
@@ -221,6 +221,11 @@ public partial class TableView
///
public static readonly DependencyProperty RowHeaderTemplateSelectorProperty = DependencyProperty.Register(nameof(RowHeaderTemplateSelector), typeof(DataTemplateSelector), typeof(TableView), new PropertyMetadata(null, OnRowHeaderTemplateChanged));
+ ///
+ /// Identifies the ColumnAutoWidthMode dependency property.
+ ///
+ public static readonly DependencyProperty ColumnAutoWidthModeProperty = DependencyProperty.Register(nameof(ColumnAutoWidthMode), typeof(ColumnAutoWidthMode), typeof(TableView), new PropertyMetadata(ColumnAutoWidthMode.Both));
+
///
/// Identifies the FrozenColumnCount dependency property.
///
@@ -725,6 +730,15 @@ public DataTemplateSelector RowHeaderTemplateSelector
set => SetValue(RowHeaderTemplateSelectorProperty, value);
}
+ ///
+ /// Gets or sets the ColumnAutoWidthMode for all columns.
+ ///
+ public ColumnAutoWidthMode ColumnAutoWidthMode
+ {
+ get => (ColumnAutoWidthMode)GetValue(ColumnAutoWidthModeProperty);
+ set => SetValue(ColumnAutoWidthModeProperty, value);
+ }
+
///
/// Gets or sets the number of columns that stays in view on horizontal scroll.
///
diff --git a/src/TableViewCell.cs b/src/TableViewCell.cs
index 758c3b14..2080033f 100644
--- a/src/TableViewCell.cs
+++ b/src/TableViewCell.cs
@@ -111,7 +111,7 @@ void OnContentLoaded(object sender, RoutedEventArgs e)
///
protected override Size MeasureOverride(Size availableSize)
{
- if (Column is not null && Row is not null && _contentPresenter is not null && Content is FrameworkElement element)
+ if (TableView is not null && Column is not null && Row is not null && _contentPresenter is not null && Content is FrameworkElement element)
{
if (Column is TableViewTemplateColumn)
{
@@ -132,16 +132,20 @@ protected override Size MeasureOverride(Size availableSize)
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
- var desiredWidth = element.DesiredSize.Width;
- desiredWidth += Padding.Left;
- desiredWidth += Padding.Right;
- desiredWidth += BorderThickness.Left;
- desiredWidth += BorderThickness.Right;
- desiredWidth += _selectionBorder?.BorderThickness.Right ?? 0;
- desiredWidth += _selectionBorder?.BorderThickness.Left ?? 0;
- desiredWidth += _v_gridLine?.ActualWidth ?? 0d;
-
- Column.DesiredWidth = Math.Max(Column.DesiredWidth, desiredWidth);
+ var autoSizeMode = Column.ColumnAutoWidthMode ?? TableView.ColumnAutoWidthMode;
+ if (autoSizeMode is ColumnAutoWidthMode.Cells or ColumnAutoWidthMode.Both)
+ {
+ var desiredWidth = element.DesiredSize.Width;
+ desiredWidth += Padding.Left;
+ desiredWidth += Padding.Right;
+ desiredWidth += BorderThickness.Left;
+ desiredWidth += BorderThickness.Right;
+ desiredWidth += _selectionBorder?.BorderThickness.Right ?? 0;
+ desiredWidth += _selectionBorder?.BorderThickness.Left ?? 0;
+ desiredWidth += _v_gridLine?.ActualWidth ?? 0d;
+
+ Column.DesiredWidth = Math.Max(Column.DesiredWidth, desiredWidth);
+ }
#region TEMP_FIX_FOR_ISSUE https://github.com/microsoft/microsoft-ui-xaml/issues/9860
var contentWidth = Column.ActualWidth;
diff --git a/src/TableViewColumnHeader.cs b/src/TableViewColumnHeader.cs
index 8a0017fe..d5a7b57f 100644
--- a/src/TableViewColumnHeader.cs
+++ b/src/TableViewColumnHeader.cs
@@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
+using Windows.Foundation;
using Windows.System;
using Windows.UI.Core;
using WinUI.TableView.Collections;
@@ -597,6 +598,23 @@ protected override void OnPointerReleased(PointerRoutedEventArgs e)
_reorderStarted = false;
}
+ ///
+ protected override Size MeasureOverride(Size availableSize)
+ {
+ var desiredHeaderSize = base.MeasureOverride(availableSize);
+
+ if (Column is not null && _tableView is not null)
+ {
+ var autoWidthMode = Column.ColumnAutoWidthMode ?? _tableView.ColumnAutoWidthMode;
+ if (autoWidthMode is ColumnAutoWidthMode.Header or ColumnAutoWidthMode.Both)
+ {
+ Column.DesiredWidth = Math.Max(Column.DesiredWidth, desiredHeaderSize.Width);
+ }
+ }
+
+ return desiredHeaderSize;
+ }
+
///
/// Ensures grid lines are applied.
///
diff --git a/src/TableViewHeaderRow.cs b/src/TableViewHeaderRow.cs
index 1224f92b..e5b776a6 100644
--- a/src/TableViewHeaderRow.cs
+++ b/src/TableViewHeaderRow.cs
@@ -289,7 +289,8 @@ internal void CalculateHeaderWidths()
var starUnitWeight = starColumns.Select(x => x.Width.Value).Sum();
var fixedWidth = autoColumns.Select(x =>
{
- if (x.HeaderControl is { } header)
+ var autoWidthMode = x.ColumnAutoWidthMode ?? TableView.ColumnAutoWidthMode;
+ if (x.HeaderControl is { } header && autoWidthMode is not ColumnAutoWidthMode.Cells)
{
header.Measure(new Size(double.PositiveInfinity, height));
return Math.Max(x.DesiredWidth, header.DesiredSize.Width);