Skip to content

Commit 97e0d39

Browse files
Poker-sangArlodotexemichael-hawker
authored
Fix: ColorPicker Binding failed (#579)
* fix ColorPicker Binding failed * Apply suggestions from code review * Update components/ColorPicker/src/Converters/NullToTransparentConverter.cs --------- Co-authored-by: Arlo <[email protected]> Co-authored-by: Michael Hawker MSFT (XAML Llama) <[email protected]>
1 parent 2c0f9b3 commit 97e0d39

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

components/ColorPicker/src/ColorPicker.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
1+
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:animations="using:CommunityToolkit.WinUI.Animations"
44
xmlns:contract8Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,8)"
@@ -16,6 +16,7 @@
1616
<SolidColorBrush x:Key="CheckerboardColor1"
1717
Color="#FFd4d4d4" />
1818
<converters:ColorToDisplayNameConverter x:Key="ColorToDisplayNameConverter" />
19+
<localconverters:NullToTransparentConverter x:Key="NullToTransparentConverter" />
1920
<localconverters:ColorToHexConverter x:Key="ColorToHexConverter" />
2021

2122
<localconverters:ContrastBrushConverter x:Key="ContrastBrushConverter" />
@@ -291,7 +292,7 @@
291292
animations:Implicit.HideAnimations="{StaticResource HideTransitions}"
292293
animations:Implicit.ShowAnimations="{StaticResource ShowTransitions}"
293294
ItemsSource="{TemplateBinding CustomPaletteColors}"
294-
SelectedValue="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Mode=TwoWay}"
295+
SelectedValue="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Converter={StaticResource NullToTransparentConverter}, Mode=TwoWay}"
295296
SelectionMode="Single"
296297
Tag="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Mode=OneWay}">
297298
<GridView.ItemsPanel>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
namespace CommunityToolkit.WinUI.Controls;
6+
7+
/// <summary>
8+
/// Value converter that converts null values to Transparent.
9+
/// </summary>
10+
public partial class NullToTransparentConverter : IValueConverter
11+
{
12+
/// <inheritdoc/>
13+
public object Convert(object value, Type targetType, object parameter, string language) => value;
14+
15+
/// <inheritdoc/>
16+
public object ConvertBack(object? value, Type targetType, object parameter, string language) => value ??
17+
#if WINUI2
18+
Windows.UI.Colors.Transparent;
19+
#else
20+
Microsoft.UI.Colors.Transparent;
21+
#endif
22+
}

0 commit comments

Comments
 (0)