diff --git a/Maui.DataGrid/CompatibilitySuppressions.xml b/Maui.DataGrid/CompatibilitySuppressions.xml
index f9f1c0f..4b25ebc 100644
--- a/Maui.DataGrid/CompatibilitySuppressions.xml
+++ b/Maui.DataGrid/CompatibilitySuppressions.xml
@@ -1,4 +1,11 @@
-
+
+ CP0001
+ T:Maui.DataGrid.Converters.BorderThicknessToCellPaddingConverter
+ lib/net8.0/Maui.DataGrid.dll
+ lib/net8.0/Maui.DataGrid.dll
+ true
+
+
\ No newline at end of file
diff --git a/Maui.DataGrid/Converters/BorderThicknessToCellPaddingConverter.cs b/Maui.DataGrid/Converters/BorderThicknessToCellPaddingConverter.cs
new file mode 100644
index 0000000..5228f33
--- /dev/null
+++ b/Maui.DataGrid/Converters/BorderThicknessToCellPaddingConverter.cs
@@ -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();
+ }
+}
diff --git a/Maui.DataGrid/DataGrid.xaml.cs b/Maui.DataGrid/DataGrid.xaml.cs
index 782eecd..d3854d8 100644
--- a/Maui.DataGrid/DataGrid.xaml.cs
+++ b/Maui.DataGrid/DataGrid.xaml.cs
@@ -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;
@@ -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 _pageSizeList = new(DefaultPageSizeSet);
@@ -687,15 +687,6 @@ internal event EventHandler RowsTextColorPaletteChanged
remove => _rowsTextColorPaletteChangedEventManager.RemoveEventHandler(value);
}
- ///
- /// Occurs when the BorderThickness of the DataGrid is changed.
- ///
- internal event EventHandler BorderThicknessChanged
- {
- add => _borderThicknessChangedEventManager.AddEventHandler(value);
- remove => _borderThicknessChangedEventManager.RemoveEventHandler(value);
- }
-
#endregion Events
#region Properties
diff --git a/Maui.DataGrid/DataGridCell.cs b/Maui.DataGrid/DataGridCell.cs
index e63f4cb..035674d 100644
--- a/Maui.DataGrid/DataGridCell.cs
+++ b/Maui.DataGrid/DataGridCell.cs
@@ -1,5 +1,6 @@
namespace Maui.DataGrid;
+using Maui.DataGrid.Converters;
using Microsoft.Maui.Controls;
///
@@ -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)
diff --git a/Maui.DataGrid/DataGridHeaderRow.cs b/Maui.DataGrid/DataGridHeaderRow.cs
index 20ecfed..786661b 100644
--- a/Maui.DataGrid/DataGridHeaderRow.cs
+++ b/Maui.DataGrid/DataGridHeaderRow.cs
@@ -47,8 +47,6 @@ internal void InitializeHeaderRow(bool force = false)
Children.Clear();
- UpdateBorders();
-
if (DataGrid.Columns == null || DataGrid.Columns.Count == 0)
{
ColumnDefinitions.Clear();
@@ -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)
@@ -118,7 +118,6 @@ protected override void OnParentSet()
if (Parent == null)
{
DataGrid.Columns.CollectionChanged -= OnColumnsChanged;
- DataGrid.BorderThicknessChanged -= OnBorderThicknessChanged;
foreach (var column in DataGrid.Columns)
{
@@ -128,7 +127,6 @@ protected override void OnParentSet()
else
{
DataGrid.Columns.CollectionChanged += OnColumnsChanged;
- DataGrid.BorderThicknessChanged += OnBorderThicknessChanged;
foreach (var column in DataGrid.Columns)
{
@@ -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();
@@ -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
}
diff --git a/Maui.DataGrid/DataGridRow.cs b/Maui.DataGrid/DataGridRow.cs
index 562a2b6..f2cdbe6 100644
--- a/Maui.DataGrid/DataGridRow.cs
+++ b/Maui.DataGrid/DataGridRow.cs
@@ -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)
{
@@ -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)
{
@@ -165,8 +163,6 @@ private void InitializeRow()
UpdateColors();
- UpdateBorders();
-
var columns = DataGrid.Columns;
if (columns == null || columns.Count == 0)
@@ -224,6 +220,8 @@ private DataGridCell GenerateCellForColumn(DataGridColumn col, int columnIndex)
{
var dataGridCell = CreateCell(col);
+ dataGridCell.UpdateBindings(DataGrid);
+
SetColumn((BindableObject)dataGridCell, columnIndex);
return dataGridCell;
@@ -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);
@@ -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();