Skip to content

Commit 5d90149

Browse files
Merge pull request #4730 from HavenDV/fix-color-picker-button-binding-issue
fix: Fix color picker button binding failure issue
2 parents acf30d6 + 7f49e98 commit 5d90149

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<localconverters:ColorToColorShadeConverter x:Key="ColorToColorShadeConverter" />
4545
<converters:ColorToDisplayNameConverter x:Key="ColorToDisplayNameConverter" />
4646
<localconverters:ColorToHexConverter x:Key="ColorToHexConverter" />
47+
<localconverters:ColorToSelectedValueConverter x:Key="ColorToSelectedValueConverter" />
4748

4849
<Style x:Key="InputTextBoxStyle"
4950
TargetType="TextBox">
@@ -157,7 +158,7 @@
157158
Padding="0"
158159
ItemContainerStyle="{StaticResource PaletteGridViewItemStyle}"
159160
ItemsSource="{TemplateBinding CustomPaletteColors}"
160-
SelectedValue="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Mode=TwoWay}"
161+
SelectedValue="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Mode=TwoWay, Converter={StaticResource ColorToSelectedValueConverter}}"
161162
SelectionMode="Single"
162163
Tag="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Mode=OneWay}">
163164
<GridView.ItemsPanel>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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;
6+
using Windows.UI.Xaml;
7+
using Windows.UI.Xaml.Data;
8+
9+
namespace Microsoft.Toolkit.Uwp.UI.Controls.ColorPickerConverters
10+
{
11+
/// <summary>
12+
/// Ignores null target values for TwoWay binding.
13+
/// </summary>
14+
public class ColorToSelectedValueConverter : IValueConverter
15+
{
16+
/// <inheritdoc/>
17+
public object Convert(
18+
object value,
19+
Type targetType,
20+
object parameter,
21+
string language)
22+
{
23+
return value;
24+
}
25+
26+
/// <inheritdoc/>
27+
public object ConvertBack(
28+
object value,
29+
Type targetType,
30+
object parameter,
31+
string language)
32+
{
33+
return value ?? DependencyProperty.UnsetValue;
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)