From bcb8588bdc04f77cf1230f9dc9f719328fba5f1e Mon Sep 17 00:00:00 2001 From: robloo Date: Tue, 21 Jun 2022 20:59:53 -0400 Subject: [PATCH] Allow setting the PaletteColors collection as an IEnumerable In WPF it was standard to have read-only collections. However, it's necessary to bind this collection between the ColorPicker and ColorView so it cannot be read-only. ItemsControl in Avalonia implements the Items property a similar way. --- .../ColorView/ColorView.Properties.cs | 16 +++++++++------- .../ColorView/ColorView.cs | 7 +++++-- .../Themes/Fluent/ColorPicker.xaml | 4 +--- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs index bed27ecb73..fcff5799c3 100644 --- a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs +++ b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs @@ -1,4 +1,5 @@ -using System.Collections.ObjectModel; +using System.Collections.Generic; +using System.Collections.ObjectModel; using Avalonia.Controls.Primitives; using Avalonia.Data; using Avalonia.Media; @@ -176,10 +177,10 @@ namespace Avalonia.Controls /// /// Defines the property. /// - public static readonly DirectProperty> PaletteColorsProperty = - AvaloniaProperty.RegisterDirect>( + public static readonly StyledProperty?> PaletteColorsProperty = + AvaloniaProperty.Register?>( nameof(PaletteColors), - o => o.PaletteColors); + null); /// /// Defines the property. @@ -392,7 +393,7 @@ namespace Avalonia.Controls } /// - /// Gets the list of individual colors in the palette. + /// Gets or sets the collection of individual colors in the palette. /// /// /// This is not commonly set manually. Instead, it should be set automatically by @@ -401,9 +402,10 @@ namespace Avalonia.Controls /// Also note that this property is what should be bound in the control template. /// is too high-level to use on its own. /// - public ObservableCollection PaletteColors + public IEnumerable? PaletteColors { - get => _paletteColors; + get => GetValue(PaletteColorsProperty); + set => SetValue(PaletteColorsProperty, value); } /// diff --git a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs index 659318edb0..4f95b4acfe 100644 --- a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs +++ b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using Avalonia.Controls.Converters; @@ -223,15 +224,17 @@ namespace Avalonia.Controls if (palette != null) { PaletteColumnCount = palette.ColorCount; - PaletteColors.Clear(); + List newPaletteColors = new List(); for (int shadeIndex = 0; shadeIndex < palette.ShadeCount; shadeIndex++) { for (int colorIndex = 0; colorIndex < palette.ColorCount; colorIndex++) { - PaletteColors.Add(palette.GetColor(colorIndex, shadeIndex)); + newPaletteColors.Add(palette.GetColor(colorIndex, shadeIndex)); } } + + PaletteColors = newPaletteColors; } } else if (change.Property == IsColorComponentsVisibleProperty || diff --git a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml index f6fb3236af..d59cd65c3d 100644 --- a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml +++ b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml @@ -45,9 +45,6 @@ -