From d66e7fb0d4b24cd1703630591d8726fc70aeb72a Mon Sep 17 00:00:00 2001 From: Poker Date: Wed, 4 Dec 2024 11:08:13 +0800 Subject: [PATCH 1/2] fix ColorPicker Binding failed --- components/ColorPicker/src/ColorPicker.xaml | 5 +++-- .../Converters/NullToTransparentConverter.cs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 components/ColorPicker/src/Converters/NullToTransparentConverter.cs diff --git a/components/ColorPicker/src/ColorPicker.xaml b/components/ColorPicker/src/ColorPicker.xaml index 284eb7b2..557f6ab0 100644 --- a/components/ColorPicker/src/ColorPicker.xaml +++ b/components/ColorPicker/src/ColorPicker.xaml @@ -1,4 +1,4 @@ - + @@ -291,7 +292,7 @@ animations:Implicit.HideAnimations="{StaticResource HideTransitions}" animations:Implicit.ShowAnimations="{StaticResource ShowTransitions}" ItemsSource="{TemplateBinding CustomPaletteColors}" - SelectedValue="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Mode=TwoWay}" + SelectedValue="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Converter={StaticResource NullToTransparentConverter}, Mode=TwoWay}" SelectionMode="Single" Tag="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Mode=OneWay}"> diff --git a/components/ColorPicker/src/Converters/NullToTransparentConverter.cs b/components/ColorPicker/src/Converters/NullToTransparentConverter.cs new file mode 100644 index 00000000..b3ad08a7 --- /dev/null +++ b/components/ColorPicker/src/Converters/NullToTransparentConverter.cs @@ -0,0 +1,19 @@ +#region Copyright +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +#endregion + +namespace CommunityToolkit.WinUI.Controls; + +internal partial class NullToTransparentConverter : IValueConverter +{ + public object Convert(object value, Type targetType, object parameter, string language) => value; + + public object ConvertBack(object? value, Type targetType, object parameter, string language) => value ?? +#if !WINAPPSDK + Windows.UI.Colors.Transparent; +#else + Microsoft.UI.Colors.Transparent; +#endif +} From c071b64502ddb6dfaea0c045c316acba43ade82c Mon Sep 17 00:00:00 2001 From: Arlo Date: Thu, 5 Dec 2024 11:23:47 -0600 Subject: [PATCH 2/2] Apply suggestions from code review --- .../ColorPicker/src/Converters/NullToTransparentConverter.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/ColorPicker/src/Converters/NullToTransparentConverter.cs b/components/ColorPicker/src/Converters/NullToTransparentConverter.cs index b3ad08a7..c4fef85c 100644 --- a/components/ColorPicker/src/Converters/NullToTransparentConverter.cs +++ b/components/ColorPicker/src/Converters/NullToTransparentConverter.cs @@ -1,8 +1,6 @@ -#region Copyright // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#endregion namespace CommunityToolkit.WinUI.Controls;