diff --git a/src/WinUI.TableView/TableView.Properties.cs b/src/WinUI.TableView/TableView.Properties.cs index 6ef80d2..87fb8f9 100644 --- a/src/WinUI.TableView/TableView.Properties.cs +++ b/src/WinUI.TableView/TableView.Properties.cs @@ -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> ActiveFilters { get; } = new Dictionary>(); @@ -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) @@ -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."); diff --git a/src/WinUI.TableView/TableView.cs b/src/WinUI.TableView/TableView.cs index 0f3c561..09036b5 100644 --- a/src/WinUI.TableView/TableView.cs +++ b/src/WinUI.TableView/TableView.cs @@ -1066,6 +1066,14 @@ private void EnsureGridLines() } } + private void EnsureAlternateRowColors() + { + foreach (var row in _rows) + { + row.EnsureAlternateColors(); + } + } + public event EventHandler? AutoGeneratingColumn; public event EventHandler? ExportAllContent; public event EventHandler? ExportSelectedContent; diff --git a/src/WinUI.TableView/TableViewRow.cs b/src/WinUI.TableView/TableViewRow.cs index bb934a8..cf0f0ae 100644 --- a/src/WinUI.TableView/TableViewRow.cs +++ b/src/WinUI.TableView/TableViewRow.cs @@ -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; @@ -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() { @@ -103,6 +106,8 @@ protected override void OnContentChanged(object oldContent, object newContent) cell.RefreshElement(); } } + + EnsureAlternateColors(); } protected override void OnPointerPressed(PointerRoutedEventArgs e) @@ -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); } @@ -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 Cells => _cellPresenter?.Cells ?? new List(); public int Index => TableView?.IndexFromContainer(this) ?? -1;