Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/ColorPicker/src/ColorPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions components/ColorPicker/tests/ColorPicker.Tests.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ExampleColorPickerTestPage.xaml.cs">
<DependentUpon>ExampleColorPickerTestPage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)PaletteSelectionTests.cs" />
</ItemGroup>
<ItemGroup>
<Page Include="$(MSBuildThisFileDirectory)ExampleColorPickerTestPage.xaml">
Expand Down
73 changes: 73 additions & 0 deletions components/ColorPicker/tests/PaletteSelectionTests.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// 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.
/// </summary>
[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<Segmented>();
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);
}
}