Skip to content

Commit

Permalink
move check to property
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Jan 25, 2024
1 parent 3233576 commit 40e11aa
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions Maui.DataGrid/DataGridRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal sealed class DataGridRow : Grid

private Color? _bgColor;
private Color? _textColor;
private bool _hasSelected;
private bool _wasSelected;

#endregion Fields

Expand Down Expand Up @@ -57,6 +57,8 @@ public object RowToEdit

private void InitializeRow()
{
UpdateSelectedState();

UpdateColors();

if (DataGrid.Columns == null || DataGrid.Columns.Count == 0)
Expand Down Expand Up @@ -131,7 +133,6 @@ private DataGridCell CreateCell(DataGridColumn col)
else
{
cellContent = CreateViewCell(col);

}

return new DataGridCell(cellContent, _bgColor, col, isEditing);
Expand Down Expand Up @@ -303,15 +304,14 @@ private DatePicker GenerateDateTimeEditCell(DataGridColumn col)

private void UpdateColors()
{
_hasSelected = DataGrid.SelectedItem == BindingContext || DataGrid.SelectedItems.Contains(BindingContext);
var rowIndex = DataGrid.InternalItems.IndexOf(BindingContext);

if (rowIndex == -1)
{
return;
}

_bgColor = DataGrid.SelectionMode != SelectionMode.None && _hasSelected
_bgColor = DataGrid.SelectionMode != SelectionMode.None && _wasSelected
? DataGrid.ActiveRowColor
: DataGrid.RowsBackgroundColorPalette.GetColor(rowIndex, BindingContext);
_textColor = DataGrid.RowsTextColorPalette.GetColor(rowIndex, BindingContext);
Expand Down Expand Up @@ -368,11 +368,17 @@ private void OnVisibilityChanged(object? sender, EventArgs e)

private void DataGrid_ItemSelected(object? sender, SelectionChangedEventArgs e)
{
if (_hasSelected || (e.CurrentSelection.Count > 0 && e.CurrentSelection.Any(s => s == BindingContext)))
if (_wasSelected || (e.CurrentSelection.Count > 0 && e.CurrentSelection.Any(s => s == BindingContext)))
{
UpdateSelectedState();
UpdateColors();
}
}

private void UpdateSelectedState()
{
_wasSelected = DataGrid.SelectedItem == BindingContext || DataGrid.SelectedItems.Contains(BindingContext);
}

#endregion Methods
}

0 comments on commit 40e11aa

Please sign in to comment.