From b7a22374428ac82cdd416f6d8f1c86534d65a933 Mon Sep 17 00:00:00 2001 From: Brett Story Date: Sun, 16 Jan 2022 13:44:30 -0600 Subject: [PATCH] Alternating ListBox control --- AvaloniaEx/Controls/AlternatingListBox.cs | 57 +++++++++++++++++++ AvaloniaEx/Theme/Accents/Base.axaml | 1 + AvaloniaEx/Theme/Base.axaml | 2 +- .../Theme/Controls/AlternatingListBox.axaml | 36 ++++++++++++ AvaloniaEx/Theme/Controls/ListBoxItem.axaml | 50 ++++++++-------- Sample/Views/MainWindow.axaml | 4 +- 6 files changed, 122 insertions(+), 28 deletions(-) create mode 100644 AvaloniaEx/Controls/AlternatingListBox.cs create mode 100644 AvaloniaEx/Theme/Controls/AlternatingListBox.axaml diff --git a/AvaloniaEx/Controls/AlternatingListBox.cs b/AvaloniaEx/Controls/AlternatingListBox.cs new file mode 100644 index 0000000..152410a --- /dev/null +++ b/AvaloniaEx/Controls/AlternatingListBox.cs @@ -0,0 +1,57 @@ +namespace Macabresoft.AvaloniaEx; + +using Avalonia; +using Avalonia.Controls; +using Avalonia.Controls.Generators; +using Avalonia.Controls.Primitives; +using Avalonia.Data; +using Avalonia.Media; + +/// +/// A with alternating row colors. +/// +public sealed class AlternatingListBox : ListBox { + /// + /// The alternate background property. + /// + public static readonly StyledProperty AlternateBackgroundProperty = + AvaloniaProperty.Register(nameof(AlternateBackground)); + + /// + /// Gets or sets the alternate background color to be used on every other row. + /// + public IBrush AlternateBackground { + get => this.GetValue(AlternateBackgroundProperty); + set => this.SetValue(AlternateBackgroundProperty, value); + } + + /// + protected override IItemContainerGenerator CreateItemContainerGenerator() { + return new AlternatingListBoxItemGenerator(this); + } + + private sealed class AlternatingListBoxItemGenerator : ItemContainerGenerator { + private readonly AlternatingListBox _owner; + private bool _useAlternateColor; + + /// + /// Initializes a new instance of the class. + /// + /// The owner. + public AlternatingListBoxItemGenerator(AlternatingListBox owner) : base(owner, ContentControl.ContentProperty, ContentControl.ContentTemplateProperty) { + this._owner = owner; + } + + /// + protected override IControl CreateContainer(object item) { + var result = base.CreateContainer(item); + if (this._useAlternateColor && result is ListBoxItem listBoxItem) { + result.SetValue(TemplatedControl.BackgroundProperty, this._owner.AlternateBackground); + } + + this._useAlternateColor = !this._useAlternateColor; + return result; + } + } +} + diff --git a/AvaloniaEx/Theme/Accents/Base.axaml b/AvaloniaEx/Theme/Accents/Base.axaml index 1b6f62e..6543192 100644 --- a/AvaloniaEx/Theme/Accents/Base.axaml +++ b/AvaloniaEx/Theme/Accents/Base.axaml @@ -47,5 +47,6 @@ + \ No newline at end of file diff --git a/AvaloniaEx/Theme/Base.axaml b/AvaloniaEx/Theme/Base.axaml index 27751c3..afafcbd 100644 --- a/AvaloniaEx/Theme/Base.axaml +++ b/AvaloniaEx/Theme/Base.axaml @@ -6,6 +6,7 @@ + @@ -26,5 +27,4 @@ - \ No newline at end of file diff --git a/AvaloniaEx/Theme/Controls/AlternatingListBox.axaml b/AvaloniaEx/Theme/Controls/AlternatingListBox.axaml new file mode 100644 index 0000000..cb49b46 --- /dev/null +++ b/AvaloniaEx/Theme/Controls/AlternatingListBox.axaml @@ -0,0 +1,36 @@ + + + \ No newline at end of file diff --git a/AvaloniaEx/Theme/Controls/ListBoxItem.axaml b/AvaloniaEx/Theme/Controls/ListBoxItem.axaml index 932af39..394ca2e 100644 --- a/AvaloniaEx/Theme/Controls/ListBoxItem.axaml +++ b/AvaloniaEx/Theme/Controls/ListBoxItem.axaml @@ -1,68 +1,68 @@ diff --git a/Sample/Views/MainWindow.axaml b/Sample/Views/MainWindow.axaml index 542abd3..a8e70fc 100644 --- a/Sample/Views/MainWindow.axaml +++ b/Sample/Views/MainWindow.axaml @@ -158,8 +158,8 @@ - +