Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Maui.DataGrid.Sample/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public MainViewModel()
TeamColumnWidth = 70;
SelectionMode = SelectionMode.Single;
PageSize = 6;
BorderThicknessNumeric = 1;
BorderThicknessNumeric = 2;

Commands.Add("CompleteEdit", new Command(CmdCompleteEdit));
Commands.Add("Edit", new Command<Team>(CmdEdit));
Expand Down
22 changes: 16 additions & 6 deletions Maui.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ 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 @@ -685,6 +686,15 @@ 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 Expand Up @@ -1103,11 +1113,11 @@ private set
#region Methods

/// <summary>
/// Scrolls to the row.
/// Scrolls to the row
/// </summary>
/// <param name="item">Item to scroll.</param>
/// <param name="position">Position of the row in screen.</param>
/// <param name="animated">animated.</param>
/// <param name="item">Item to scroll</param>
/// <param name="position">Position of the row in screen</param>
/// <param name="animated">animated</param>
public void ScrollTo(object item, ScrollToPosition position, bool animated = true) => _collectionView.ScrollTo(item, position: position, animate: animated);

internal void Initialize()
Expand Down Expand Up @@ -1366,11 +1376,11 @@ private IEnumerable<object> GetPaginatedItems(IEnumerable<object> unpaginatedIte
}

/// <summary>
/// Checks if PageSizeList contains the new PageSize value, so that it shows in the dropdown.
/// Checks if PageSizeList contains the new PageSize value, so that it shows in the dropdown
/// </summary>
private void UpdatePageSizeList()
{
if (_pageSizeList.Contains(PageSize))
if (PageSizeList.Contains(PageSize))
{
return;
}
Expand Down
16 changes: 0 additions & 16 deletions Maui.DataGrid/DataGridCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ internal DataGridCell(View cellContent, Color? backgroundColor, DataGridColumn c

public bool IsEditing { get; }

internal void UpdateBindings(DataGrid dataGrid, bool bordersVisible = true)
{
if (bordersVisible)
{
SetBinding(BackgroundColorProperty, new Binding(nameof(DataGrid.BorderColor), source: dataGrid));
SetBinding(PaddingProperty, new Binding(nameof(DataGrid.BorderThickness), source: dataGrid));
}
else
{
RemoveBinding(BackgroundColorProperty);
RemoveBinding(PaddingProperty);

Padding = 0;
}
}

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

Children.Clear();

UpdateBorders();

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

col.HeaderCell ??= CreateHeaderCell(col);

col.HeaderCell.UpdateBindings(DataGrid, DataGrid.HeaderBordersVisible);

if (Children.TryGetItem(i, out var existingChild))
{
if (existingChild is not DataGridCell existingCell)
Expand All @@ -103,6 +103,29 @@ internal void InitializeHeaderRow(bool force = false)
ColumnDefinitions.RemoveAfter(DataGrid.Columns.Count);
}

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;
}
}

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

/// <inheritdoc/>
protected override void OnBindingContextChanged()
{
Expand All @@ -118,6 +141,7 @@ protected override void OnParentSet()
if (Parent == null)
{
DataGrid.Columns.CollectionChanged -= OnColumnsChanged;
DataGrid.BorderThicknessChanged -= OnBorderThicknessChanged;

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

foreach (var column in DataGrid.Columns)
{
column.VisibilityChanged += OnVisibilityChanged;
}

SetBinding(BackgroundColorProperty, new Binding(nameof(DataGrid.BorderColor), source: DataGrid));
}
}

Expand Down
24 changes: 22 additions & 2 deletions Maui.DataGrid/DataGridRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ protected override void OnBindingContextChanged()
InitializeRow();
}


/// <inheritdoc/>
protected override void OnParentSet()
{
Expand All @@ -127,6 +128,7 @@ 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 @@ -139,11 +141,14 @@ protected override void OnParentSet()
DataGrid.Columns.CollectionChanged += OnColumnsChanged;
DataGrid.RowsBackgroundColorPaletteChanged += OnRowsBackgroundColorPaletteChanged;
DataGrid.RowsTextColorPaletteChanged += OnRowsTextColorPaletteChanged;
DataGrid.BorderThicknessChanged += OnBorderThicknessChanged;

foreach (var column in DataGrid.Columns)
{
column.VisibilityChanged += OnVisibilityChanged;
}

SetBinding(BackgroundColorProperty, new Binding(nameof(DataGrid.BorderColor), source: DataGrid));
}
}

Expand All @@ -161,6 +166,8 @@ private void InitializeRow()

UpdateColors();

UpdateBorders();

var columns = DataGrid.Columns;

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

dataGridCell.UpdateBindings(DataGrid);

SetColumn((BindableObject)dataGridCell, columnIndex);

return dataGridCell;
Expand Down Expand Up @@ -409,6 +414,16 @@ 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 @@ -428,6 +443,11 @@ 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