From 6725b0bf2bb00c020930ac7051d02f58e833c8f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20R=C3=BChe?= <471338+Marv51@users.noreply.github.com> Date: Thu, 12 Mar 2026 11:56:29 +0100 Subject: [PATCH 1/2] In ColorPicker, update PaletteItemGridView when selected color changed (fixes #833) --- components/ColorPicker/src/ColorPicker.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/ColorPicker/src/ColorPicker.cs b/components/ColorPicker/src/ColorPicker.cs index f03bdcb8..dbf74ba6 100644 --- a/components/ColorPicker/src/ColorPicker.cs +++ b/components/ColorPicker/src/ColorPicker.cs @@ -764,6 +764,11 @@ private void UpdateColorControlValues() } } + if (this.PaletteItemGridView != null) + { + this.PaletteItemGridView.SelectedItem = (this.CustomPaletteColors?.Contains(rgbColor) ?? false) ? rgbColor : null; + } + if (eventsDisconnectedByMethod) { this.ConnectEvents(true); From ce15f68549d8818318c0dfc42cb2a774b35fea40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20R=C3=BChe?= <471338+Marv51@users.noreply.github.com> Date: Thu, 12 Mar 2026 15:14:21 +0100 Subject: [PATCH 2/2] Add regression test --- .../tests/ColorPicker.Tests.projitems | 1 + .../tests/PaletteSelectionTests.cs | 73 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 components/ColorPicker/tests/PaletteSelectionTests.cs diff --git a/components/ColorPicker/tests/ColorPicker.Tests.projitems b/components/ColorPicker/tests/ColorPicker.Tests.projitems index 534c1b27..09afdab8 100644 --- a/components/ColorPicker/tests/ColorPicker.Tests.projitems +++ b/components/ColorPicker/tests/ColorPicker.Tests.projitems @@ -13,6 +13,7 @@ ExampleColorPickerTestPage.xaml + diff --git a/components/ColorPicker/tests/PaletteSelectionTests.cs b/components/ColorPicker/tests/PaletteSelectionTests.cs new file mode 100644 index 00000000..c6531ee5 --- /dev/null +++ b/components/ColorPicker/tests/PaletteSelectionTests.cs @@ -0,0 +1,73 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using CommunityToolkit.Tooling.TestGen; +using CommunityToolkit.Tests; +using CommunityToolkit.WinUI.Controls; +using Microsoft.UI.Xaml.Controls; +using ColorPicker = CommunityToolkit.WinUI.Controls.ColorPicker; + +namespace ColorPickerTests; + +[TestClass] +public partial class PaletteSelectionTests : VisualUITestBase +{ + // First color in FluentColorPalette (#ffb900) + private static readonly Windows.UI.Color PaletteColor = Windows.UI.Color.FromArgb(255, 255, 185, 0); + + // Second palette color for the round-trip check (#d13438) + private static readonly Windows.UI.Color PaletteColor2 = Windows.UI.Color.FromArgb(255, 209, 52, 56); + + // White is not in the FluentColorPalette + private static readonly Windows.UI.Color NonPaletteColor = Colors.White; + + /// + /// Verifies that the palette GridView selection follows the Color property when it is changed + /// programmatically (e.g. via the spectrum or sliders). Regression test for issue #833. + /// + [UIThreadTestMethod] + public async Task PaletteGridView_FollowsColorProperty() + { + var colorPicker = new ColorPicker(); + await LoadTestContentAsync(colorPicker); + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); + + // The PaletteItemGridView lives inside a SwitchPresenter Case that is only added to + // the visual tree once the palette tab is selected. Navigate there first. + var panelSelector = colorPicker.FindDescendant(); + Assert.IsNotNull(panelSelector, "Could not find ColorPanelSelector in visual tree."); + + var paletteItem = panelSelector.FindDescendant("PaletteItem") as SegmentedItem; + Assert.IsNotNull(paletteItem, "Could not find PaletteItem in ColorPanelSelector."); + + panelSelector.SelectedItem = paletteItem; + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); + + var paletteGridView = colorPicker.FindDescendant("PaletteItemGridView") as GridView; + Assert.IsNotNull(paletteGridView, "Could not find PaletteItemGridView in visual tree."); + + // Set to a color that is in the default palette — selection should follow. + colorPicker.Color = PaletteColor; + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); + + Assert.AreEqual(PaletteColor, paletteGridView.SelectedItem, + "PaletteItemGridView should select the matching palette color when Color is set."); + + // Set to a color not in the palette — selection should be cleared. + colorPicker.Color = NonPaletteColor; + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); + + Assert.IsNull(paletteGridView.SelectedItem, + "PaletteItemGridView should have no selection when Color is not in the palette."); + + // Set to a different palette color — selection should update to match. + colorPicker.Color = PaletteColor2; + await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); + + Assert.AreEqual(PaletteColor2, paletteGridView.SelectedItem, + "PaletteItemGridView should update its selection when Color changes to another palette color."); + + await UnloadTestContentAsync(colorPicker); + } +}