-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// A <see cref="ListBox"/> with alternating row colors. | ||
/// </summary> | ||
public sealed class AlternatingListBox : ListBox { | ||
/// <summary> | ||
/// The alternate background property. | ||
/// </summary> | ||
public static readonly StyledProperty<IBrush> AlternateBackgroundProperty = | ||
AvaloniaProperty.Register<AlternatingListBox, IBrush>(nameof(AlternateBackground)); | ||
|
||
/// <summary> | ||
/// Gets or sets the alternate background color to be used on every other row. | ||
/// </summary> | ||
public IBrush AlternateBackground { | ||
get => this.GetValue(AlternateBackgroundProperty); | ||
set => this.SetValue(AlternateBackgroundProperty, value); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override IItemContainerGenerator CreateItemContainerGenerator() { | ||
return new AlternatingListBoxItemGenerator(this); | ||
} | ||
|
||
private sealed class AlternatingListBoxItemGenerator : ItemContainerGenerator<ListBoxItem> { | ||
private readonly AlternatingListBox _owner; | ||
private bool _useAlternateColor; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AlternatingListBoxItemGenerator" /> class. | ||
/// </summary> | ||
/// <param name="owner">The owner.</param> | ||
public AlternatingListBoxItemGenerator(AlternatingListBox owner) : base(owner, ContentControl.ContentProperty, ContentControl.ContentTemplateProperty) { | ||
this._owner = owner; | ||
} | ||
|
||
/// <inheritdoc /> | ||
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; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<Styles xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:avaloniaEx="clr-namespace:Macabresoft.AvaloniaEx"> | ||
<Style Selector="avaloniaEx|AlternatingListBox"> | ||
<Setter Property="TextBlock.Foreground" Value="{StaticResource SystemControlForegroundBaseHighBrush}" /> | ||
<Setter Property="Background" Value="Transparent" /> | ||
<Setter Property="AlternateBackground" Value="{StaticResource AlternateListBoxItemBackgroundColor}" /> | ||
<Setter Property="BorderBrush" Value="{StaticResource SystemControlForegroundBaseHighBrush}" /> | ||
<Setter Property="BorderThickness" Value="{StaticResource ListBoxBorderThemeThickness}" /> | ||
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" /> | ||
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /> | ||
<Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}" /> | ||
<Setter Property="Margin" Value="{StaticResource StandardMarginAll}" /> | ||
<Setter Property="Template"> | ||
<ControlTemplate> | ||
<Border Name="border" | ||
Background="{TemplateBinding Background}" | ||
BorderBrush="{TemplateBinding BorderBrush}" | ||
BorderThickness="{TemplateBinding BorderThickness}" | ||
CornerRadius="{TemplateBinding CornerRadius}"> | ||
<ScrollViewer Name="PART_ScrollViewer" | ||
HorizontalScrollBarVisibility="{TemplateBinding (ScrollViewer.HorizontalScrollBarVisibility)}" | ||
VerticalScrollBarVisibility="{TemplateBinding (ScrollViewer.VerticalScrollBarVisibility)}" | ||
AllowAutoHide="{TemplateBinding (ScrollViewer.AllowAutoHide)}"> | ||
<ItemsPresenter Name="PART_ItemsPresenter" | ||
Items="{TemplateBinding Items}" | ||
ItemsPanel="{TemplateBinding ItemsPanel}" | ||
ItemTemplate="{TemplateBinding ItemTemplate}" | ||
Margin="{TemplateBinding Padding}" | ||
VirtualizationMode="{TemplateBinding VirtualizationMode}"/> | ||
</ScrollViewer> | ||
</Border> | ||
</ControlTemplate> | ||
</Setter> | ||
</Style> | ||
</Styles> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters