Skip to content

Commit 50180e7

Browse files
committed
measure cell content height to fix cell bounds
1 parent 8cf7ded commit 50180e7

File tree

2 files changed

+61
-38
lines changed

2 files changed

+61
-38
lines changed

src/TableViewCell.cs

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public partial class TableViewCell : ContentControl
2929
private ContentPresenter? _contentPresenter;
3030
private Border? _selectionBorder;
3131
private Rectangle? _v_gridLine;
32+
private TableViewCellsPresenter? _cellPresenter;
3233

3334
/// <summary>
3435
/// Initializes a new instance of the TableViewCell class.
@@ -43,7 +44,6 @@ public TableViewCell()
4344
#endif
4445
}
4546

46-
4747
#if !WINDOWS
4848
protected override void OnRightTapped(RightTappedRoutedEventArgs e)
4949
{
@@ -84,56 +84,62 @@ protected override void OnApplyTemplate()
8484

8585
protected override Size MeasureOverride(Size availableSize)
8686
{
87-
if (Content is FrameworkElement element)
87+
if (Column is not null && Row is not null && _contentPresenter is not null && Content is FrameworkElement element)
8888
{
89-
var v_GridLineStrokeThickness = TableView?.HeaderGridLinesVisibility is TableViewGridLinesVisibility.All or TableViewGridLinesVisibility.Vertical
90-
|| TableView?.GridLinesVisibility is TableViewGridLinesVisibility.All or TableViewGridLinesVisibility.Vertical
91-
? TableView.VerticalGridLinesStrokeThickness : 0;
89+
#if WINDOWS
90+
#region TEMP_FIX_FOR_ISSUE https://github.com/microsoft/microsoft-ui-xaml/issues/9860
91+
element.MaxWidth = double.PositiveInfinity;
92+
element.MaxHeight = double.PositiveInfinity;
93+
#endregion
94+
#endif
95+
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
96+
97+
var desiredWidth = element.DesiredSize.Width;
98+
desiredWidth += Padding.Left;
99+
desiredWidth += Padding.Right;
100+
desiredWidth += BorderThickness.Left;
101+
desiredWidth += BorderThickness.Right;
102+
desiredWidth += _selectionBorder?.BorderThickness.Right ?? 0;
103+
desiredWidth += _selectionBorder?.BorderThickness.Left ?? 0;
104+
desiredWidth += _v_gridLine?.ActualWidth ?? 0d;
105+
106+
Column.DesiredWidth = Math.Max(Column.DesiredWidth, desiredWidth);
107+
92108
#if WINDOWS
93109
#region TEMP_FIX_FOR_ISSUE https://github.com/microsoft/microsoft-ui-xaml/issues/9860
94-
var contentWidth = Column?.ActualWidth ?? 0d;
110+
var contentWidth = Column.ActualWidth;
95111
contentWidth -= element.Margin.Left;
96112
contentWidth -= element.Margin.Right;
97113
contentWidth -= Padding.Left;
98114
contentWidth -= Padding.Right;
99115
contentWidth -= BorderThickness.Left;
100116
contentWidth -= BorderThickness.Right;
101-
contentWidth -= _selectionBorder?.BorderThickness.Right ?? 0;
102117
contentWidth -= _selectionBorder?.BorderThickness.Left ?? 0;
103-
contentWidth -= v_GridLineStrokeThickness;
104-
105-
element.MaxWidth = double.PositiveInfinity;
106-
#endregion
107-
#endif
108-
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
109-
110-
if (Column is not null)
118+
contentWidth -= _selectionBorder?.BorderThickness.Right ?? 0;
119+
contentWidth -= _v_gridLine?.ActualWidth ?? 0d;
120+
121+
var rowHeight = Row.Height is double.NaN ? double.PositiveInfinity : Row.Height;
122+
var rowMaxHeight = Row.MaxHeight;
123+
var contentHeight = Math.Min(rowHeight, rowMaxHeight);
124+
contentHeight -= element.Margin.Top;
125+
contentHeight -= element.Margin.Bottom;
126+
contentHeight -= Padding.Top;
127+
contentHeight -= Padding.Bottom;
128+
contentHeight -= BorderThickness.Top;
129+
contentHeight -= BorderThickness.Bottom;
130+
contentHeight -= _selectionBorder?.BorderThickness.Top ?? 0;
131+
contentHeight -= _selectionBorder?.BorderThickness.Bottom ?? 0;
132+
contentHeight -= GetHorizonalGridlineHeight();
133+
134+
if (contentWidth < 0 || contentHeight < 0)
111135
{
112-
var desiredWidth = element.DesiredSize.Width;
113-
desiredWidth += Padding.Left;
114-
desiredWidth += Padding.Right;
115-
desiredWidth += BorderThickness.Left;
116-
desiredWidth += BorderThickness.Right;
117-
desiredWidth += _selectionBorder?.BorderThickness.Right ?? 0;
118-
desiredWidth += _selectionBorder?.BorderThickness.Left ?? 0;
119-
desiredWidth += v_GridLineStrokeThickness;
120-
121-
Column.DesiredWidth = Math.Max(Column.DesiredWidth, desiredWidth);
136+
_contentPresenter.Visibility = Visibility.Collapsed;
122137
}
123-
124-
#if WINDOWS
125-
#region TEMP_FIX_FOR_ISSUE https://github.com/microsoft/microsoft-ui-xaml/issues/9860
126-
if (_contentPresenter is not null)
138+
else
127139
{
128-
if (contentWidth < 0)
129-
{
130-
_contentPresenter.Visibility = Visibility.Collapsed;
131-
}
132-
else
133-
{
134-
element.MaxWidth = contentWidth;
135-
_contentPresenter.Visibility = Visibility.Visible;
136-
}
140+
element.MaxWidth = contentWidth;
141+
element.MaxHeight = contentHeight;
142+
_contentPresenter.Visibility = Visibility.Visible;
137143
}
138144
#endregion
139145
#endif
@@ -142,6 +148,15 @@ protected override Size MeasureOverride(Size availableSize)
142148
return base.MeasureOverride(availableSize);
143149
}
144150

151+
/// <summary>
152+
/// Retrieves the height of the horizontal gridline.
153+
/// </summary>
154+
private double GetHorizonalGridlineHeight()
155+
{
156+
_cellPresenter ??= this?.FindAscendant<TableViewCellsPresenter>();
157+
return _cellPresenter?.GetHorizonalGridlineHeight() ?? 0d;
158+
}
159+
145160
protected override void OnPointerEntered(PointerRoutedEventArgs e)
146161
{
147162
base.OnPointerEntered(e);

src/TableViewCellsPresenter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ internal void EnsureGridLines()
7676
}
7777
}
7878

79+
/// <summary>
80+
/// Retrieves the height of the horizontal gridline.
81+
/// </summary>
82+
internal double GetHorizonalGridlineHeight()
83+
{
84+
return _h_gridLine?.ActualHeight ?? 0d;
85+
}
86+
7987
/// <summary>
8088
/// Gets the collection of child elements.
8189
/// </summary>

0 commit comments

Comments
 (0)