From 631a0eda452328b32ca3eea69949192e8e23c96d Mon Sep 17 00:00:00 2001 From: Dong Bin <14807942+rabbitism@users.noreply.github.com> Date: Mon, 2 Oct 2023 12:14:40 +0800 Subject: [PATCH] Make ColorPicker button content customizable. (#13073) * feat: add Content and ContentPresenter for ColorPicker * feat: update according to https://github.com/AvaloniaUI/Avalonia/pull/13073 feedback. * feat: add HorizontalContentAlignment and VerticalContentAlignment default value to simple theme. * feat: update documentation wording. --------- Co-authored-by: Max Katz --- .../ControlCatalog/Pages/ColorPickerPage.xaml | 34 +++++++-- .../ColorPicker/ColorPicker.cs | 75 ++++++++++++++++++- .../Themes/Fluent/ColorPicker.xaml | 40 +++++----- .../Themes/Simple/ColorPicker.xaml | 40 +++++----- 4 files changed, 143 insertions(+), 46 deletions(-) diff --git a/samples/ControlCatalog/Pages/ColorPickerPage.xaml b/samples/ControlCatalog/Pages/ColorPickerPage.xaml index 25fffabfd2..fb397085e7 100644 --- a/samples/ControlCatalog/Pages/ColorPickerPage.xaml +++ b/samples/ControlCatalog/Pages/ColorPickerPage.xaml @@ -3,12 +3,14 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="using:Avalonia.Controls" + xmlns:converters="clr-namespace:Avalonia.Markup.Xaml.Converters;assembly=Avalonia.Markup.Xaml" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="ControlCatalog.Pages.ColorPickerPage"> + - - - - - + + + + + + + + + + + + + + + diff --git a/src/Avalonia.Controls.ColorPicker/ColorPicker/ColorPicker.cs b/src/Avalonia.Controls.ColorPicker/ColorPicker/ColorPicker.cs index 92d8535272..aa573bf3d3 100644 --- a/src/Avalonia.Controls.ColorPicker/ColorPicker/ColorPicker.cs +++ b/src/Avalonia.Controls.ColorPicker/ColorPicker/ColorPicker.cs @@ -1,8 +1,14 @@ -namespace Avalonia.Controls +using Avalonia.Controls.Metadata; +using Avalonia.Controls.Primitives; +using Avalonia.Controls.Templates; +using Avalonia.Layout; +using Avalonia.Metadata; + +namespace Avalonia.Controls { /// /// Presents a color for user editing using a spectrum, palette and component sliders within a drop down. - /// Editing is available when the drop down flyout is opened; otherwise, only the preview color is shown. + /// Editing is available when the drop down flyout is opened; otherwise, only the preview content area is shown. /// public class ColorPicker : ColorView { @@ -12,5 +18,70 @@ public ColorPicker() : base() { } + + /// + /// Defines the property. + /// + public static readonly StyledProperty ContentProperty = + ContentControl.ContentProperty.AddOwner(); + + /// + /// Defines the property. + /// + public static readonly StyledProperty ContentTemplateProperty = + ContentControl.ContentTemplateProperty.AddOwner(); + + /// + /// Defines the property. + /// + public static readonly StyledProperty HorizontalContentAlignmentProperty = + ContentControl.HorizontalContentAlignmentProperty.AddOwner(); + + /// + /// Defines the property. + /// + public static readonly StyledProperty VerticalContentAlignmentProperty = + ContentControl.VerticalContentAlignmentProperty.AddOwner(); + + /// + /// Gets or sets any content displayed in the ColorPicker's preview content area. + /// + /// + /// By default this should show a preview of the currently selected color. + /// + [Content] + [DependsOn(nameof(ContentTemplate))] + public object? Content + { + get => GetValue(ContentProperty); + set => SetValue(ContentProperty, value); + } + + /// + /// Gets or sets the data template used to display the content of the ColorPicker's preview content area. + /// + public IDataTemplate? ContentTemplate + { + get => GetValue(ContentTemplateProperty); + set => SetValue(ContentTemplateProperty, value); + } + + /// + /// Gets or sets the horizontal alignment of the content within the ColorPicker's preview content area. + /// + public HorizontalAlignment HorizontalContentAlignment + { + get => GetValue(HorizontalContentAlignmentProperty); + set => SetValue(HorizontalContentAlignmentProperty, value); + } + + /// + /// Gets or sets the vertical alignment of the content within the ColorPicker's preview content area. + /// + public VerticalAlignment VerticalContentAlignment + { + get => GetValue(VerticalContentAlignmentProperty); + set => SetValue(VerticalContentAlignmentProperty, value); + } } } diff --git a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml index 104314e54f..f051d9bbab 100644 --- a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml +++ b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml @@ -12,6 +12,24 @@ + + + + + @@ -20,8 +38,10 @@ @@ -29,21 +49,6 @@ - - - - - - - @@ -519,7 +524,6 @@ --> - diff --git a/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPicker.xaml b/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPicker.xaml index c20f7935f4..4fbb7a290a 100644 --- a/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPicker.xaml +++ b/src/Avalonia.Controls.ColorPicker/Themes/Simple/ColorPicker.xaml @@ -12,6 +12,24 @@ + + + + + @@ -20,8 +38,10 @@ @@ -29,21 +49,6 @@ - - - - - - - @@ -518,7 +523,6 @@ --> -