Skip to content

Commit dde93f3

Browse files
committed
Add central OnDependencyPropertyChanged method in ColorPicker
1 parent e23a895 commit dde93f3

File tree

2 files changed

+57
-53
lines changed

2 files changed

+57
-53
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public partial class ColorPicker
2020
nameof(CustomPaletteColors),
2121
typeof(ObservableCollection<Windows.UI.Color>),
2222
typeof(ColorPicker),
23-
new PropertyMetadata(null));
23+
new PropertyMetadata(
24+
null,
25+
(s, e) => (s as ColorPicker)?.OnDependencyPropertyChanged(s, e)));
2426

2527
/// <summary>
2628
/// Gets the list of custom palette colors.
@@ -38,7 +40,9 @@ public ObservableCollection<Windows.UI.Color> CustomPaletteColors
3840
nameof(CustomPaletteColumnCount),
3941
typeof(int),
4042
typeof(ColorPicker),
41-
new PropertyMetadata(4));
43+
new PropertyMetadata(
44+
4,
45+
(s, e) => (s as ColorPicker)?.OnDependencyPropertyChanged(s, e)));
4246

4347
/// <summary>
4448
/// Gets or sets the number of colors in each row (section) of the custom color palette.
@@ -64,7 +68,9 @@ public int CustomPaletteColumnCount
6468
nameof(CustomPalette),
6569
typeof(IColorPalette),
6670
typeof(ColorPicker),
67-
new PropertyMetadata(null));
71+
new PropertyMetadata(
72+
null,
73+
(s, e) => (s as ColorPicker)?.OnDependencyPropertyChanged(s, e)));
6874

6975
/// <summary>
7076
/// Gets or sets the custom color palette.
@@ -91,7 +97,9 @@ public IColorPalette CustomPalette
9197
nameof(IsColorPaletteVisible),
9298
typeof(bool),
9399
typeof(ColorPicker),
94-
new PropertyMetadata(true));
100+
new PropertyMetadata(
101+
true,
102+
(s, e) => (s as ColorPicker)?.OnDependencyPropertyChanged(s, e)));
95103

96104
/// <summary>
97105
/// Gets or sets a value indicating whether the color palette is visible.

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

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ public partial class ColorPicker : Microsoft.UI.Xaml.Controls.ColorPicker
6666
private const int ColorUpdateInterval = 30; // Milliseconds
6767

6868
private long tokenColor;
69-
private long tokenCustomPalette;
70-
private long tokenIsColorPaletteVisible;
7169

7270
private bool callbacksConnected = false;
7371
private bool eventsConnected = false;
@@ -252,23 +250,19 @@ private T GetTemplateChild<T>(string childName, bool isRequired = false)
252250
/// <param name="connected">True to connect callbacks, otherwise false.</param>
253251
private void ConnectCallbacks(bool connected)
254252
{
255-
if ((connected == true) &&
256-
(this.callbacksConnected == false))
253+
if (connected == true &&
254+
this.callbacksConnected == false)
257255
{
258256
// Add callbacks for dependency properties
259-
this.tokenColor = this.RegisterPropertyChangedCallback(ColorProperty, OnColorChanged);
260-
this.tokenCustomPalette = this.RegisterPropertyChangedCallback(CustomPaletteProperty, OnCustomPaletteChanged);
261-
this.tokenIsColorPaletteVisible = this.RegisterPropertyChangedCallback(IsColorPaletteVisibleProperty, OnIsColorPaletteVisibleChanged);
257+
this.tokenColor = this.RegisterPropertyChangedCallback(ColorProperty, OnColorChanged);
262258

263259
this.callbacksConnected = true;
264260
}
265-
else if ((connected == false) &&
266-
(this.callbacksConnected == true))
261+
else if (connected == false &&
262+
this.callbacksConnected == true)
267263
{
268264
// Remove callbacks for dependency properties
269-
this.UnregisterPropertyChangedCallback(ColorProperty, this.tokenColor);
270-
this.UnregisterPropertyChangedCallback(CustomPaletteProperty, this.tokenCustomPalette);
271-
this.UnregisterPropertyChangedCallback(IsColorPaletteVisibleProperty, this.tokenIsColorPaletteVisible);
265+
this.UnregisterPropertyChangedCallback(ColorProperty, this.tokenColor);
272266

273267
this.callbacksConnected = false;
274268
}
@@ -282,8 +276,8 @@ private void ConnectCallbacks(bool connected)
282276
/// <param name="connected">True to connect event handlers, otherwise false.</param>
283277
private void ConnectEvents(bool connected)
284278
{
285-
if ((connected == true) &&
286-
(this.eventsConnected == false))
279+
if (connected == true &&
280+
this.eventsConnected == false)
287281
{
288282
// Add all events
289283
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.ColorChanged += ColorSpectrum_ColorChanged; }
@@ -336,8 +330,8 @@ private void ConnectEvents(bool connected)
336330

337331
this.eventsConnected = true;
338332
}
339-
else if ((connected == false) &&
340-
(this.eventsConnected == true))
333+
else if (connected == false &&
334+
this.eventsConnected == true)
341335
{
342336
// Remove all events
343337
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.ColorChanged -= ColorSpectrum_ColorChanged; }
@@ -1112,6 +1106,41 @@ private void ValidateSelectedPanel()
11121106
return;
11131107
}
11141108

1109+
private void OnDependencyPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
1110+
{
1111+
DependencyObject senderControl = sender as DependencyObject;
1112+
1113+
/* Note: ColorProperty is defined in the base class and cannot be used here
1114+
* See the OnColorChanged callback below
1115+
*/
1116+
1117+
if (object.ReferenceEquals(args.Property, CustomPaletteProperty))
1118+
{
1119+
IColorPalette palette = this.CustomPalette;
1120+
1121+
if (palette != null)
1122+
{
1123+
this.CustomPaletteColumnCount = palette.ColorCount;
1124+
this.CustomPaletteColors.Clear();
1125+
1126+
for (int shadeIndex = 0; shadeIndex < palette.ShadeCount; shadeIndex++)
1127+
{
1128+
for (int colorIndex = 0; colorIndex < palette.ColorCount; colorIndex++)
1129+
{
1130+
this.CustomPaletteColors.Add(palette.GetColor(colorIndex, shadeIndex));
1131+
}
1132+
}
1133+
}
1134+
}
1135+
else if (object.ReferenceEquals(args.Property, IsColorPaletteVisibleProperty))
1136+
{
1137+
this.UpdateVisualState(false);
1138+
this.ValidateSelectedPanel();
1139+
}
1140+
1141+
return;
1142+
}
1143+
11151144
/***************************************************************************************
11161145
*
11171146
* Color Update Timer
@@ -1194,39 +1223,6 @@ private void OnColorChanged(DependencyObject d, DependencyProperty e)
11941223
return;
11951224
}
11961225

1197-
/// <summary>
1198-
/// Callback for when the <see cref="CustomPalette"/> dependency property value changes.
1199-
/// </summary>
1200-
private void OnCustomPaletteChanged(DependencyObject d, DependencyProperty e)
1201-
{
1202-
IColorPalette palette = this.CustomPalette;
1203-
1204-
if (palette != null)
1205-
{
1206-
this.CustomPaletteColumnCount = palette.ColorCount;
1207-
this.CustomPaletteColors.Clear();
1208-
1209-
for (int shadeIndex = 0; shadeIndex < palette.ShadeCount; shadeIndex++)
1210-
{
1211-
for (int colorIndex = 0; colorIndex < palette.ColorCount; colorIndex++)
1212-
{
1213-
this.CustomPaletteColors.Add(palette.GetColor(colorIndex, shadeIndex));
1214-
}
1215-
}
1216-
}
1217-
1218-
return;
1219-
}
1220-
1221-
/// <summary>
1222-
/// Callback for when the <see cref="IsColorPaletteVisible"/> dependency property value changes.
1223-
/// </summary>
1224-
private void OnIsColorPaletteVisibleChanged(DependencyObject d, DependencyProperty e)
1225-
{
1226-
this.UpdateVisualState(false);
1227-
return;
1228-
}
1229-
12301226
/***************************************************************************************
12311227
*
12321228
* Event Handling

0 commit comments

Comments
 (0)