Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ColorPicker Binding failed #579

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions components/ColorPicker/src/ColorPicker.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:animations="using:CommunityToolkit.WinUI.Animations"
xmlns:contract8Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,8)"
Expand All @@ -16,6 +16,7 @@
<SolidColorBrush x:Key="CheckerboardColor1"
Color="#FFd4d4d4" />
<converters:ColorToDisplayNameConverter x:Key="ColorToDisplayNameConverter" />
<localconverters:NullToTransparentConverter x:Key="NullToTransparentConverter" />
<localconverters:ColorToHexConverter x:Key="ColorToHexConverter" />

<localconverters:ContrastBrushConverter x:Key="ContrastBrushConverter" />
Expand Down Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is TargetNullValue not sufficient here for some reason?

Copy link
Contributor Author

@Poker-sang Poker-sang Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the null problem arises with BindBack and not Bind, I guess TargetNullValue won't help.

Tag="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color, Mode=OneWay}">
<GridView.ItemsPanel>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

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
}
Loading