Skip to content

Commit f8edf76

Browse files
authored
Merge pull request #165 from w-ahmad/measure_issue
fixed cell and row measure issues
2 parents d683d7d + 50180e7 commit f8edf76

File tree

3 files changed

+61
-52
lines changed

3 files changed

+61
-52
lines changed

src/TableView.Properties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public partial class TableView
3333
/// <summary>
3434
/// Identifies the RowHeight dependency property.
3535
/// </summary>
36-
public static readonly DependencyProperty RowHeightProperty = DependencyProperty.Register(nameof(RowHeight), typeof(double), typeof(TableView), new PropertyMetadata(40d));
36+
public static readonly DependencyProperty RowHeightProperty = DependencyProperty.Register(nameof(RowHeight), typeof(double), typeof(TableView), new PropertyMetadata(double.NaN));
3737

3838
/// <summary>
3939
/// Identifies the RowMaxHeight dependency property.

src/TableViewCell.cs

Lines changed: 52 additions & 51 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,70 +84,62 @@ protected override void OnApplyTemplate()
8484

8585
protected override Size MeasureOverride(Size availableSize)
8686
{
87-
if ((Content ?? ContentTemplateRoot) 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;
9289
#if WINDOWS
93-
#region TEMP_FIX_FOR_ISSUE https://github.com/microsoft/microsoft-ui-xaml/issues/9860
94-
if (Column is TableViewTemplateColumn)
95-
{
96-
if (element is ContentControl contentControl &&
97-
(contentControl.Content ?? contentControl.ContentTemplateRoot) is FrameworkElement contentElement)
98-
{
99-
element = contentElement;
100-
}
101-
else
102-
{
103-
return base.MeasureOverride(availableSize);
104-
}
105-
}
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;
106105

106+
Column.DesiredWidth = Math.Max(Column.DesiredWidth, desiredWidth);
107107

108-
var contentWidth = Column?.ActualWidth ?? 0d;
108+
#if WINDOWS
109+
#region TEMP_FIX_FOR_ISSUE https://github.com/microsoft/microsoft-ui-xaml/issues/9860
110+
var contentWidth = Column.ActualWidth;
109111
contentWidth -= element.Margin.Left;
110112
contentWidth -= element.Margin.Right;
111113
contentWidth -= Padding.Left;
112114
contentWidth -= Padding.Right;
113115
contentWidth -= BorderThickness.Left;
114116
contentWidth -= BorderThickness.Right;
115-
contentWidth -= _selectionBorder?.BorderThickness.Right ?? 0;
116117
contentWidth -= _selectionBorder?.BorderThickness.Left ?? 0;
117-
contentWidth -= v_GridLineStrokeThickness;
118-
119-
element.MaxWidth = double.PositiveInfinity;
120-
#endregion
121-
#endif
122-
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
123-
124-
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)
125135
{
126-
var desiredWidth = element.DesiredSize.Width;
127-
desiredWidth += Padding.Left;
128-
desiredWidth += Padding.Right;
129-
desiredWidth += BorderThickness.Left;
130-
desiredWidth += BorderThickness.Right;
131-
desiredWidth += _selectionBorder?.BorderThickness.Right ?? 0;
132-
desiredWidth += _selectionBorder?.BorderThickness.Left ?? 0;
133-
desiredWidth += v_GridLineStrokeThickness;
134-
135-
Column.DesiredWidth = Math.Max(Column.DesiredWidth, desiredWidth);
136+
_contentPresenter.Visibility = Visibility.Collapsed;
136137
}
137-
138-
#if WINDOWS
139-
#region TEMP_FIX_FOR_ISSUE https://github.com/microsoft/microsoft-ui-xaml/issues/9860
140-
if (_contentPresenter is not null)
138+
else
141139
{
142-
if (contentWidth < 0)
143-
{
144-
_contentPresenter.Visibility = Visibility.Collapsed;
145-
}
146-
else
147-
{
148-
element.MaxWidth = contentWidth;
149-
_contentPresenter.Visibility = Visibility.Visible;
150-
}
140+
element.MaxWidth = contentWidth;
141+
element.MaxHeight = contentHeight;
142+
_contentPresenter.Visibility = Visibility.Visible;
151143
}
152144
#endregion
153145
#endif
@@ -156,6 +148,15 @@ protected override Size MeasureOverride(Size availableSize)
156148
return base.MeasureOverride(availableSize);
157149
}
158150

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+
159160
protected override void OnPointerEntered(PointerRoutedEventArgs e)
160161
{
161162
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)