Skip to content

Commit f209425

Browse files
AndrewKeepCodingArlodotexe
authored andcommitted
Remove binding to prevent unintended value update from hidden color palette
The color palette GridView was applying a selection even when not visible, causing the bound SelectedColor to reset unexpectedly. This change replaces the binding with code-behind value update to preserve the intended value when the palette is collapsed.
1 parent 3d7d396 commit f209425

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

components/ColorPicker/src/ColorPicker.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace CommunityToolkit.WinUI.Controls;
4646
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumControl), Type = typeof(ColorSpectrum))]
4747
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumAlphaSlider), Type = typeof(ColorPickerSlider))]
4848
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumThirdDimensionSlider), Type = typeof(ColorPickerSlider))]
49+
[TemplatePart(Name = nameof(ColorPicker.PaletteItemGridView), Type = typeof(GridView))]
4950
[TemplatePart(Name = nameof(ColorPicker.HexInputTextBox), Type = typeof(TextBox))]
5051
[TemplatePart(Name = nameof(ColorPicker.ColorModeComboBox), Type = typeof(ComboBox))]
5152

@@ -81,6 +82,7 @@ public partial class ColorPicker : Microsoft.UI.Xaml.Controls.ColorPicker
8182
private ColorSpectrum ColorSpectrumControl;
8283
private ColorPickerSlider ColorSpectrumAlphaSlider;
8384
private ColorPickerSlider ColorSpectrumThirdDimensionSlider;
85+
private GridView PaletteItemGridView;
8486
private TextBox HexInputTextBox;
8587
private ComboBox ColorModeComboBox;
8688

@@ -186,6 +188,8 @@ protected override void OnApplyTemplate()
186188
this.ColorSpectrumAlphaSlider = (ColorPickerSlider)this.GetTemplateChild(nameof(ColorSpectrumAlphaSlider));
187189
this.ColorSpectrumThirdDimensionSlider = (ColorPickerSlider)this.GetTemplateChild(nameof(ColorSpectrumThirdDimensionSlider));
188190

191+
this.PaletteItemGridView = (GridView)GetTemplateChild(nameof(PaletteItemGridView));
192+
189193
this.HexInputTextBox = (TextBox)this.GetTemplateChild(nameof(HexInputTextBox));
190194
this.ColorModeComboBox = (ComboBox)this.GetTemplateChild(nameof(ColorModeComboBox));
191195

@@ -263,6 +267,9 @@ private void ConnectEvents(bool connected)
263267

264268
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.ColorChanged += ColorSpectrum_ColorChanged; }
265269
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.GotFocus += ColorSpectrum_GotFocus; }
270+
271+
if (this.PaletteItemGridView != null) { this.PaletteItemGridView.SelectionChanged += this.PaletteItemGridView_SelectionChanged; }
272+
266273
if (this.HexInputTextBox != null) { this.HexInputTextBox.KeyDown += HexInputTextBox_KeyDown; }
267274
if (this.HexInputTextBox != null) { this.HexInputTextBox.LostFocus += HexInputTextBox_LostFocus; }
268275
if (this.ColorModeComboBox != null) { this.ColorModeComboBox.SelectionChanged += ColorModeComboBox_SelectionChanged; }
@@ -309,6 +316,9 @@ private void ConnectEvents(bool connected)
309316

310317
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.ColorChanged -= ColorSpectrum_ColorChanged; }
311318
if (this.ColorSpectrumControl != null) { this.ColorSpectrumControl.GotFocus -= ColorSpectrum_GotFocus; }
319+
320+
if (this.PaletteItemGridView != null) { this.PaletteItemGridView.SelectionChanged -= this.PaletteItemGridView_SelectionChanged; }
321+
312322
if (this.HexInputTextBox != null) { this.HexInputTextBox.KeyDown -= HexInputTextBox_KeyDown; }
313323
if (this.HexInputTextBox != null) { this.HexInputTextBox.LostFocus -= HexInputTextBox_LostFocus; }
314324
if (this.ColorModeComboBox != null) { this.ColorModeComboBox.SelectionChanged -= ColorModeComboBox_SelectionChanged; }
@@ -1325,6 +1335,20 @@ private void ColorSpectrum_GotFocus(object sender, RoutedEventArgs e)
13251335
return;
13261336
}
13271337

1338+
/// <summary>
1339+
/// Event handler for when a color is selected from the palette.
1340+
/// This will update the current color.
1341+
/// </summary>
1342+
private void PaletteItemGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
1343+
{
1344+
if ((sender as GridView)?.SelectedValue is not Color selectedColor)
1345+
{
1346+
return;
1347+
}
1348+
1349+
this.Color = selectedColor;
1350+
}
1351+
13281352
/// <summary>
13291353
/// Event handler for when the selected color representation changes.
13301354
/// This will convert between RGB and HSV.

components/ColorPicker/src/ColorPicker.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,12 @@
286286
</controls:Case>
287287
<controls:Case Value="PaletteItem">
288288
<Grid HorizontalAlignment="Stretch">
289-
<GridView Margin="0"
289+
<GridView x:Name="PaletteItemGridView"
290+
Margin="0"
290291
Padding="0"
291292
animations:Implicit.HideAnimations="{StaticResource HideTransitions}"
292293
animations:Implicit.ShowAnimations="{StaticResource ShowTransitions}"
293294
ItemsSource="{TemplateBinding CustomPaletteColors}"
294-
SelectedValue="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Converter={StaticResource NullToTransparentConverter}, Mode=TwoWay}"
295295
SelectionMode="Single"
296296
Tag="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Mode=OneWay}">
297297
<GridView.ItemsPanel>

0 commit comments

Comments
 (0)