Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/ColumnAutoWidthMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace WinUI.TableView;

/// <summary>
/// Specifies the behavior for automatic column width.
/// </summary>
public enum ColumnAutoWidthMode
{
/// <summary>
/// Column width is adjusted to both header and maximum cell width.
/// </summary>
Both,

/// <summary>
/// Column width is adjusted to maximum cell width.
/// </summary>
Cells,

/// <summary>
/// Column width is adjusted to header width.
/// </summary>
Header
}
14 changes: 14 additions & 0 deletions src/Columns/TableViewColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ public double ActualWidth
set => SetValue(ActualWidthProperty, value);
}

/// <summary>
/// Gets or sets the ColumnAutoWidthMode of the column.
/// </summary>
public ColumnAutoWidthMode? ColumnAutoWidthMode
{
get => (ColumnAutoWidthMode?)GetValue(ColumnAutoWidthModeProperty);
set => SetValue(ColumnAutoWidthModeProperty, value);
}

/// <summary>
/// Gets or sets a value indicating whether the column can be resized.
/// </summary>
Expand Down Expand Up @@ -504,6 +513,11 @@ public string? SortMemberPath
/// </summary>
public static readonly DependencyProperty ActualWidthProperty = DependencyProperty.Register(nameof(ActualWidth), typeof(double), typeof(TableViewColumn), new PropertyMetadata(0d, OnPropertyChanged));

/// <summary>
/// Identifies the ActualWidth dependency property.
/// </summary>
public static readonly DependencyProperty ColumnAutoWidthModeProperty = DependencyProperty.Register(nameof(ColumnAutoWidthMode), typeof(ColumnAutoWidthMode?), typeof(TableViewColumn), new PropertyMetadata(null));
Comment thread
Mangepange marked this conversation as resolved.

/// <summary>
/// Identifies the CanResize dependency property.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/TableView.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ public partial class TableView
/// </summary>
public static readonly DependencyProperty RowHeaderTemplateSelectorProperty = DependencyProperty.Register(nameof(RowHeaderTemplateSelector), typeof(DataTemplateSelector), typeof(TableView), new PropertyMetadata(null, OnRowHeaderTemplateChanged));

/// <summary>
/// Identifies the ColumnAutoWidthMode dependency property.
/// </summary>
public static readonly DependencyProperty ColumnAutoWidthModeProperty = DependencyProperty.Register(nameof(ColumnAutoWidthMode), typeof(ColumnAutoWidthMode), typeof(TableView), new PropertyMetadata(ColumnAutoWidthMode.Both));

/// <summary>
/// Identifies the FrozenColumnCount dependency property.
/// </summary>
Expand Down Expand Up @@ -725,6 +730,15 @@ public DataTemplateSelector RowHeaderTemplateSelector
set => SetValue(RowHeaderTemplateSelectorProperty, value);
}

/// <summary>
/// Gets or sets the ColumnAutoWidthMode for all columns.
/// </summary>
public ColumnAutoWidthMode ColumnAutoWidthMode
{
get => (ColumnAutoWidthMode)GetValue(ColumnAutoWidthModeProperty);
set => SetValue(ColumnAutoWidthModeProperty, value);
}

/// <summary>
/// Gets or sets the number of columns that stays in view on horizontal scroll.
/// </summary>
Expand Down
26 changes: 15 additions & 11 deletions src/TableViewCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void OnContentLoaded(object sender, RoutedEventArgs e)
/// <inheritdoc/>
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)
{
Expand All @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions src/TableViewColumnHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -597,6 +598,23 @@ protected override void OnPointerReleased(PointerRoutedEventArgs e)
_reorderStarted = false;
}

/// <inheritdoc/>
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;
}

/// <summary>
/// Ensures grid lines are applied.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/TableViewHeaderRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading