Skip to content

Commit 4fd5702

Browse files
Merge pull request #4372 from Marv51/colorpickerbutton-fix
Fix ColorPickerButton SelectedColor bug
2 parents 1e7776e + 8fcb64a commit 4fd5702

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-1
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPickerButton.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public Color SelectedColor
8181
/// Identifies the <see cref="SelectedColor"/> dependency property.
8282
/// </summary>
8383
public static readonly DependencyProperty SelectedColorProperty =
84-
DependencyProperty.Register(nameof(SelectedColor), typeof(Color), typeof(ColorPickerButton), new PropertyMetadata(null));
84+
DependencyProperty.Register(nameof(SelectedColor), typeof(Color), typeof(ColorPickerButton), new PropertyMetadata(null, new PropertyChangedCallback(SelectedColorChanged)));
8585

8686
#pragma warning disable SA1306 // Field names should begin with lower-case letter
8787
//// Template Parts
@@ -142,6 +142,14 @@ protected override void OnApplyTemplate()
142142
}
143143
}
144144

145+
private static void SelectedColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
146+
{
147+
if (d is ColorPickerButton instance && !(instance.ColorPicker is null))
148+
{
149+
instance.ColorPicker.Color = instance.SelectedColor;
150+
}
151+
}
152+
145153
private void ColorPicker_ColorChanged(Microsoft.UI.Xaml.Controls.ColorPicker sender, Microsoft.UI.Xaml.Controls.ColorChangedEventArgs args)
146154
{
147155
SelectedColor = args.NewColor;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Windows.Apps.Test.Foundation.Controls;
6+
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Common;
7+
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Infra;
8+
9+
#if USING_TAEF
10+
using WEX.Logging.Interop;
11+
using WEX.TestExecution;
12+
using WEX.TestExecution.Markup;
13+
#else
14+
using Microsoft.VisualStudio.TestTools.UnitTesting;
15+
#endif
16+
17+
namespace UITests.Tests
18+
{
19+
[TestClass]
20+
public class ColorPickerButtonTest : UITestBase
21+
{
22+
[ClassInitialize]
23+
[TestProperty("RunAs", "User")]
24+
[TestProperty("Classification", "ScenarioTestSuite")]
25+
[TestProperty("Platform", "Any")]
26+
public static void ClassInitialize(TestContext testContext)
27+
{
28+
TestEnvironment.Initialize(testContext, WinUICsUWPSampleApp);
29+
}
30+
31+
/// <summary>
32+
/// This test validates the two way binding of the selected color. It verifies that
33+
/// when the bound property changes this change is properly forwarded to the internal colorpicker.
34+
/// See also issue #4367
35+
/// </summary>
36+
[TestMethod]
37+
[TestPage("ColorPickerButtonTestPage")]
38+
public void TwoWayTestMethod()
39+
{
40+
var colorpicker = new Button(FindElement.ById("TheColorPickerButton"));
41+
42+
var redButton = new Button(FindElement.ById("SetRedButton"));
43+
44+
Verify.IsNotNull(colorpicker);
45+
Verify.IsNotNull(redButton);
46+
47+
colorpicker.Click();
48+
49+
Wait.ForIdle();
50+
var colorInput = GetColorPickerInputField();
51+
52+
Verify.AreEqual("008000", colorInput.GetText());
53+
54+
// close the picker
55+
colorpicker.Click();
56+
57+
Wait.ForIdle();
58+
59+
redButton.Click();
60+
61+
Wait.ForIdle();
62+
63+
colorpicker.Click();
64+
65+
var colorInput_new = GetColorPickerInputField();
66+
Verify.AreEqual("FF0000", colorInput_new.GetText());
67+
}
68+
69+
private static Edit GetColorPickerInputField()
70+
{
71+
var channelButton = new Button(FindElement.ByName("Channels"));
72+
Verify.IsNotNull(channelButton);
73+
74+
Wait.ForIdle();
75+
76+
channelButton.Click();
77+
78+
Wait.ForIdle();
79+
80+
var colorInput = new Edit(FindElement.ByName("Hexadecimal Color Input"));
81+
Verify.IsNotNull(colorInput);
82+
return colorInput;
83+
}
84+
}
85+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Page x:Class="UITests.App.Pages.ColorPickerButtonTestPage"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
7+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
8+
mc:Ignorable="d">
9+
<StackPanel>
10+
<controls:ColorPickerButton x:Name="TheColorPickerButton" SelectedColor="{x:Bind TheColor, Mode=TwoWay}" />
11+
<Button Name="SetRedButton" Click="Button_Click">Set color to red</Button>
12+
</StackPanel>
13+
</Page>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.ComponentModel;
6+
using Windows.UI;
7+
using Windows.UI.Xaml;
8+
using Windows.UI.Xaml.Controls;
9+
10+
namespace UITests.App.Pages
11+
{
12+
/// <summary>
13+
/// An empty page that can be used on its own or navigated to within a Frame.
14+
/// </summary>
15+
public sealed partial class ColorPickerButtonTestPage : Page, INotifyPropertyChanged
16+
{
17+
private Color _theColor = Colors.Green;
18+
19+
public Color TheColor
20+
{
21+
get => _theColor;
22+
set
23+
{
24+
if (_theColor != value)
25+
{
26+
_theColor = value;
27+
OnPropertyChanged(nameof(TheColor));
28+
}
29+
}
30+
}
31+
32+
public ColorPickerButtonTestPage()
33+
{
34+
DataContext = this;
35+
this.InitializeComponent();
36+
}
37+
38+
public event PropertyChangedEventHandler PropertyChanged;
39+
40+
private void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
41+
42+
private void Button_Click(object sender, RoutedEventArgs e)
43+
{
44+
TheColor = Colors.Red;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)