Skip to content

Commit

Permalink
dynamic border thickness
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Sep 28, 2024
1 parent 54f12d3 commit 186da49
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 57 deletions.
9 changes: 8 additions & 1 deletion Maui.DataGrid/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</Suppressions>
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Maui.DataGrid.Converters.BorderThicknessToCellPaddingConverter</Target>
<Left>lib/net8.0/Maui.DataGrid.dll</Left>
<Right>lib/net8.0/Maui.DataGrid.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
</Suppressions>
21 changes: 21 additions & 0 deletions Maui.DataGrid/Converters/BorderThicknessToCellPaddingConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Maui.DataGrid.Converters;

using System.Globalization;

public class BorderThicknessToCellPaddingConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Thickness thickness)
{
return new Thickness(thickness.Left / 2, thickness.Top / 2, thickness.Right / 2, thickness.Bottom / 2);
}

return new Thickness(0);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
11 changes: 1 addition & 10 deletions Maui.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Maui.DataGrid;
using System.Diagnostics;
using System.Windows.Input;
using Maui.DataGrid.Collections;
using Maui.DataGrid.Converters;
using Maui.DataGrid.Extensions;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Shapes;
Expand Down Expand Up @@ -618,7 +619,6 @@ public partial class DataGrid
private readonly WeakEventManager _refreshingEventManager = new();
private readonly WeakEventManager _rowsBackgroundColorPaletteChangedEventManager = new();
private readonly WeakEventManager _rowsTextColorPaletteChangedEventManager = new();
private readonly WeakEventManager _borderThicknessChangedEventManager = new();

private readonly SortedSet<int> _pageSizeList = new(DefaultPageSizeSet);

Expand Down Expand Up @@ -687,15 +687,6 @@ internal event EventHandler RowsTextColorPaletteChanged
remove => _rowsTextColorPaletteChangedEventManager.RemoveEventHandler(value);
}

/// <summary>
/// Occurs when the BorderThickness of the DataGrid is changed.
/// </summary>
internal event EventHandler BorderThicknessChanged
{
add => _borderThicknessChangedEventManager.AddEventHandler(value);
remove => _borderThicknessChangedEventManager.RemoveEventHandler(value);
}

#endregion Events

#region Properties
Expand Down
20 changes: 20 additions & 0 deletions Maui.DataGrid/DataGridCell.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Maui.DataGrid;

using Maui.DataGrid.Converters;
using Microsoft.Maui.Controls;

/// <summary>
Expand All @@ -23,6 +24,25 @@ internal DataGridCell(View cellContent, Color? backgroundColor, DataGridColumn c

public bool IsEditing { get; }

internal void UpdateBindings(DataGrid dataGrid)
{
// This approach is a hack to avoid needing a slow Border control.
// The padding constitutes the cell's border thickness.
// And the BackgroundColor constitutes the border color of the cell.
if (dataGrid.HeaderBordersVisible)
{
SetBinding(BackgroundColorProperty, new Binding(nameof(DataGrid.BorderColor), source: dataGrid));
SetBinding(PaddingProperty, new Binding(nameof(DataGrid.BorderThickness), converter: new BorderThicknessToCellPaddingConverter(), source: dataGrid));
}
else
{
RemoveBinding(BackgroundColorProperty);
RemoveBinding(PaddingProperty);

Padding = 0;
}
}

internal void UpdateCellBackgroundColor(Color? bgColor)
{
foreach (var child in Children)
Expand Down
29 changes: 2 additions & 27 deletions Maui.DataGrid/DataGridHeaderRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ internal void InitializeHeaderRow(bool force = false)

Children.Clear();

UpdateBorders();

if (DataGrid.Columns == null || DataGrid.Columns.Count == 0)
{
ColumnDefinitions.Clear();
Expand Down Expand Up @@ -79,6 +77,8 @@ internal void InitializeHeaderRow(bool force = false)

col.HeaderCell ??= CreateHeaderCell(col);

col.HeaderCell.UpdateBindings(DataGrid);

if (Children.TryGetItem(i, out var existingChild))
{
if (existingChild is not DataGridCell existingCell)
Expand Down Expand Up @@ -118,7 +118,6 @@ protected override void OnParentSet()
if (Parent == null)
{
DataGrid.Columns.CollectionChanged -= OnColumnsChanged;
DataGrid.BorderThicknessChanged -= OnBorderThicknessChanged;

foreach (var column in DataGrid.Columns)
{
Expand All @@ -128,7 +127,6 @@ protected override void OnParentSet()
else
{
DataGrid.Columns.CollectionChanged += OnColumnsChanged;
DataGrid.BorderThicknessChanged += OnBorderThicknessChanged;

foreach (var column in DataGrid.Columns)
{
Expand Down Expand Up @@ -163,11 +161,6 @@ private static bool CanSort(DataGridColumn column)
return column.SortingEnabled && column.DataGrid.Columns.Contains(column);
}

private void OnBorderThicknessChanged(object? sender, EventArgs e)
{
UpdateBorders();
}

private void OnColumnsChanged(object? sender, EventArgs e)
{
InitializeHeaderRow();
Expand Down Expand Up @@ -217,23 +210,5 @@ private DataGridCell CreateHeaderCell(DataGridColumn column)
return new DataGridCell(cellContent, DataGrid.HeaderBackground, column, false);
}

private void UpdateBorders()
{
// This approach is a hack to avoid needing a slow Border control.
// The padding constitutes the cell's border thickness.
// And the BackgroundColor constitutes the border color of the cell.
if (DataGrid.HeaderBordersVisible)
{
var borderSize = DataGrid.BorderThickness;
ColumnSpacing = borderSize.Left;
Padding = new(0, borderSize.Top / 2, 0, borderSize.Bottom / 2);
}
else
{
ColumnSpacing = 0;
Padding = 0;
}
}

#endregion Methods
}
21 changes: 2 additions & 19 deletions Maui.DataGrid/DataGridRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ protected override void OnParentSet()
DataGrid.Columns.CollectionChanged -= OnColumnsChanged;
DataGrid.RowsBackgroundColorPaletteChanged -= OnRowsBackgroundColorPaletteChanged;
DataGrid.RowsTextColorPaletteChanged -= OnRowsTextColorPaletteChanged;
DataGrid.BorderThicknessChanged -= OnBorderThicknessChanged;

foreach (var column in DataGrid.Columns)
{
Expand All @@ -140,7 +139,6 @@ protected override void OnParentSet()
DataGrid.Columns.CollectionChanged += OnColumnsChanged;
DataGrid.RowsBackgroundColorPaletteChanged += OnRowsBackgroundColorPaletteChanged;
DataGrid.RowsTextColorPaletteChanged += OnRowsTextColorPaletteChanged;
DataGrid.BorderThicknessChanged += OnBorderThicknessChanged;

foreach (var column in DataGrid.Columns)
{
Expand All @@ -165,8 +163,6 @@ private void InitializeRow()

UpdateColors();

UpdateBorders();

var columns = DataGrid.Columns;

if (columns == null || columns.Count == 0)
Expand Down Expand Up @@ -224,6 +220,8 @@ private DataGridCell GenerateCellForColumn(DataGridColumn col, int columnIndex)
{
var dataGridCell = CreateCell(col);

dataGridCell.UpdateBindings(DataGrid);

SetColumn((BindableObject)dataGridCell, columnIndex);

return dataGridCell;
Expand Down Expand Up @@ -413,16 +411,6 @@ private DatePicker GenerateDateTimeEditCell(DataGridColumn col)
return datePicker;
}

private void UpdateBorders()
{
// This approach is a hack to avoid needing a slow Border control.
// The padding constitutes the cell's border thickness.
// And the BackgroundColor constitutes the border color of the cell.
var borderSize = DataGrid.BorderThickness;
ColumnSpacing = borderSize.Left;
Padding = new(0, borderSize.Top / 2, 0, borderSize.Bottom / 2);
}

private void UpdateColors()
{
var rowIndex = DataGrid.InternalItems.IndexOf(BindingContext);
Expand All @@ -442,11 +430,6 @@ private void UpdateColors()
: DataGrid.RowsTextColorPalette.GetColor(rowIndex, BindingContext);
}

private void OnBorderThicknessChanged(object? sender, EventArgs e)
{
UpdateBorders();
}

private void OnRowsTextColorPaletteChanged(object? sender, EventArgs e)
{
UpdateColors();
Expand Down

0 comments on commit 186da49

Please sign in to comment.