diff --git a/src/WinUI.TableView/TableViewCell.cs b/src/WinUI.TableView/TableViewCell.cs index 0cf056d..0b1e617 100644 --- a/src/WinUI.TableView/TableViewCell.cs +++ b/src/WinUI.TableView/TableViewCell.cs @@ -1,10 +1,7 @@ -using CommunityToolkit.WinUI; -using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; -using Microsoft.UI.Xaml.Media; using System; -using System.Linq; using System.Threading.Tasks; using Windows.Foundation; @@ -33,9 +30,28 @@ protected override Size MeasureOverride(Size availableSize) { var size = base.MeasureOverride(availableSize); - if (Column is not null) + if ((Content ?? ContentTemplateRoot) is FrameworkElement element) { - Column.DesiredWidth = Math.Max(Column.DesiredWidth, size.Width); + var contentWidth = Column.ActualWidth; + contentWidth -= element.Margin.Left; + contentWidth -= element.Margin.Right; + contentWidth -= Padding.Left; + contentWidth -= Padding.Right; + contentWidth -= BorderThickness.Left; + contentWidth -= BorderThickness.Right; + + element.MaxWidth = contentWidth; + + if (Column is not null) + { + var desiredWidth = element.DesiredSize.Width; + desiredWidth += Padding.Left; + desiredWidth += Padding.Right; + desiredWidth += BorderThickness.Left; + desiredWidth += BorderThickness.Right; + + Column.DesiredWidth = Math.Max(Column.DesiredWidth, desiredWidth); + } } return size; @@ -180,7 +196,7 @@ internal void ApplyCurrentCellState() private static void OnColumnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { - if (d is TableViewCell cell && e.NewValue is TableViewColumn column) + if (d is TableViewCell cell) { if (cell.TableView?.IsEditing == true) { diff --git a/src/WinUI.TableView/TableViewRow.cs b/src/WinUI.TableView/TableViewRow.cs index ea983d7..f319609 100644 --- a/src/WinUI.TableView/TableViewRow.cs +++ b/src/WinUI.TableView/TableViewRow.cs @@ -1,11 +1,13 @@ using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Data; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; namespace WinUI.TableView; + public class TableViewRow : ListViewItem { private TableViewCellPresenter? _cellPresenter; @@ -71,13 +73,6 @@ private void OnColumnPropertyChanged(object? sender, TableViewColumnPropertyChan RemoveCells(new[] { e.Column }); } } - else if (e.PropertyName is nameof(TableViewColumn.ActualWidth)) - { - if (Cells.FirstOrDefault(x => x.Column == e.Column) is { } cell) - { - cell.Width = e.Column.ActualWidth; - } - } } private void RemoveCells(IEnumerable columns) @@ -107,9 +102,15 @@ private void AddCells(IEnumerable columns, int index = -1) Column = column, TableView = TableView!, Index = TableView.Columns.VisibleColumns.IndexOf(column), - Width = column.ActualWidth }; + cell.SetBinding(WidthProperty, new Binding + { + Path = new PropertyPath(nameof(TableViewColumn.ActualWidth)), + Source = column, + Mode = BindingMode.OneWay + }); + if (index < 0) { _cellPresenter.Children.Add(cell);