Skip to content

Commit

Permalink
Merge pull request #83 from w-ahmad/feature/AlternateRowColors
Browse files Browse the repository at this point in the history
added AlternateRowBackground/Foreground properties
  • Loading branch information
w-ahmad authored Dec 13, 2024
2 parents 3b56c9c + 5c74ec0 commit 644b866
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/WinUI.TableView/TableView.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public partial class TableView
public static readonly DependencyProperty VerticalGridLinesStrokeThicknessProperty = DependencyProperty.Register(nameof(VerticalGridLinesStrokeThickness), typeof(double), typeof(TableView), new PropertyMetadata(1d, OnGridLinesPropertyChanged));
public static readonly DependencyProperty HorizontalGridLinesStrokeProperty = DependencyProperty.Register(nameof(HorizontalGridLinesStroke), typeof(Brush), typeof(TableView), new PropertyMetadata(default, OnGridLinesPropertyChanged));
public static readonly DependencyProperty VerticalGridLinesStrokeProperty = DependencyProperty.Register(nameof(VerticalGridLinesStroke), typeof(Brush), typeof(TableView), new PropertyMetadata(default, OnGridLinesPropertyChanged));
public static readonly DependencyProperty AlternateRowForegroundProperty = DependencyProperty.Register(nameof(AlternateRowForeground), typeof(Brush), typeof(TableView), new PropertyMetadata(null, OnAlternateRowColorChanged));
public static readonly DependencyProperty AlternateRowBackgroundProperty = DependencyProperty.Register(nameof(AlternateRowBackground), typeof(Brush), typeof(TableView), new PropertyMetadata(null, OnAlternateRowColorChanged));

public IAdvancedCollectionView CollectionView { get; private set; } = new AdvancedCollectionView();
internal IDictionary<string, Predicate<object>> ActiveFilters { get; } = new Dictionary<string, Predicate<object>>();
Expand Down Expand Up @@ -170,6 +172,18 @@ public Brush HorizontalGridLinesStroke
set => SetValue(HorizontalGridLinesStrokeProperty, value);
}

public Brush AlternateRowBackground
{
get => (Brush)GetValue(AlternateRowBackgroundProperty);
set => SetValue(AlternateRowBackgroundProperty, value);
}

public Brush AlternateRowForeground
{
get => (Brush)GetValue(AlternateRowForegroundProperty);
set => SetValue(AlternateRowForegroundProperty, value);
}

private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableView tableView)
Expand Down Expand Up @@ -291,6 +305,14 @@ private static void OnGridLinesPropertyChanged(DependencyObject d, DependencyPro
}
}

private static void OnAlternateRowColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableView tableView)
{
tableView.EnsureAlternateRowColors();
}
}

private void OnBaseItemsSourceChanged(DependencyObject sender, DependencyProperty dp)
{
throw new InvalidOperationException("Setting this property directly is not allowed. Use TableView.ItemsSource instead.");
Expand Down
8 changes: 8 additions & 0 deletions src/WinUI.TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,14 @@ private void EnsureGridLines()
}
}

private void EnsureAlternateRowColors()
{
foreach (var row in _rows)
{
row.EnsureAlternateColors();
}
}

public event EventHandler<TableViewAutoGeneratingColumnEventArgs>? AutoGeneratingColumn;
public event EventHandler<TableViewExportContentEventArgs>? ExportAllContent;
public event EventHandler<TableViewExportContentEventArgs>? ExportSelectedContent;
Expand Down
20 changes: 20 additions & 0 deletions src/WinUI.TableView/TableViewRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
Expand All @@ -22,6 +23,8 @@ public class TableViewRow : ListViewItem
private TableViewCellsPresenter? _cellPresenter;
private bool _ensureCells = true;
private Border? _selectionBackground;
private Brush? _cellPresenterBackground;
private Brush? _cellPresenterForeground;

public TableViewRow()
{
Expand Down Expand Up @@ -103,6 +106,8 @@ protected override void OnContentChanged(object oldContent, object newContent)
cell.RefreshElement();
}
}

EnsureAlternateColors();
}

protected override void OnPointerPressed(PointerRoutedEventArgs e)
Expand Down Expand Up @@ -145,9 +150,13 @@ private void EnsureCells()
}

_cellPresenter = ContentTemplateRoot as TableViewCellsPresenter;

if (_cellPresenter is not null)
{
_cellPresenterBackground = _cellPresenter.Background;
_cellPresenterForeground = _cellPresenter.Foreground;
_cellPresenter.Children.Clear();

AddCells(TableView.Columns.VisibleColumns);
}

Expand Down Expand Up @@ -347,6 +356,17 @@ internal void EnsureLayout()
}
}

internal void EnsureAlternateColors()
{
if (TableView is null || _cellPresenter is null) return;

_cellPresenter.Background =
Index % 2 == 1 && TableView.AlternateRowBackground is not null ? TableView.AlternateRowBackground : _cellPresenterBackground;

_cellPresenter.Foreground =
Index % 2 == 1 && TableView.AlternateRowForeground is not null ? TableView.AlternateRowForeground : _cellPresenterForeground;
}

internal IList<TableViewCell> Cells => _cellPresenter?.Cells ?? new List<TableViewCell>();

public int Index => TableView?.IndexFromContainer(this) ?? -1;
Expand Down

0 comments on commit 644b866

Please sign in to comment.