Skip to content

Commit

Permalink
fixed cell layout issue
Browse files Browse the repository at this point in the history
  • Loading branch information
w-ahmad committed Jul 6, 2024
1 parent 01a706b commit fd59014
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
30 changes: 23 additions & 7 deletions src/WinUI.TableView/TableViewCell.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
17 changes: 9 additions & 8 deletions src/WinUI.TableView/TableViewRow.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<TableViewColumn> columns)
Expand Down Expand Up @@ -107,9 +102,15 @@ private void AddCells(IEnumerable<TableViewColumn> 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);
Expand Down

0 comments on commit fd59014

Please sign in to comment.