WindowsCommunityToolkit
WindowsCommunityToolkit copied to clipboard
ColorPickerButton Binding errors
Describe the bug
<controls:ColorPickerButton SelectedColor="{x:Bind ViewModel.CustomColor, Mode=TwoWay, Converter={StaticResource SystemDrawingColorToColorConverter}}"/>
I am getting the following error messages: However, everything seems to work.
What's the converter code you're using? It seems like SelectedColor is probably null to start with, getting passed in to your converter, which is throwing an exception (and caught by the XAML stack)? Check the output window for any thrown Exceptions that might not be enabled in Visual Studio's exception settings. If all else fails, create/use an empty converter and put a breakpoint in it to see what the actual value being passed is Target confuses me, as if it's some kind of internal converter.
Error: Converter failed to convert value of type 'null' to type 'Color'; BindingExpression: Path='Color' DataItem='CommunityToolkit.WinUI.UI.Controls.ColorPicker'; target element is 'Microsoft.UI.Xaml.Controls.GridView' (Name='null'); target property is 'SelectedValue' (type 'Object').
Error: Converter failed to convert value of type 'null' to type 'Color'; BindingExpression: Path='Color' DataItem='CommunityToolkit.WinUI.UI.Controls.ColorPicker'; target element is 'Microsoft.UI.Xaml.Controls.GridView' (Name='null'); target property is 'SelectedValue' (type 'Object').
These errors occur at the moment the button is clicked, at this moment the converter is not called. This is preceded by a call to IValueConverter.Convert with a non-null value, as is the initial initialization.
I wasn't able to check the values via ColorToDisplayNameConverter because the symbols seem to be missing
While not likely related to this package, I can see pdb files inside. Most likely, I'm doing something wrong, because debugging only works for
<DebugType>embedded</DebugType>
libraries for me.
Regression
No response
Reproducible in sample app?
- [ ] This bug can be reproduced in the sample app.
Steps to reproduce
<controls:ColorPickerButton SelectedColor="{x:Bind ViewModel.CustomColor, Mode=TwoWay, Converter={StaticResource SystemDrawingColorToColorConverter}}"/>
Expected behavior
Without errors
Screenshots
https://user-images.githubusercontent.com/3002068/184275847-d88f5e0e-f224-429f-9471-fa872345a0de.mp4
No response
Windows Build Number
- [X] Windows 10 1809 (Build 17763)
- [ ] Windows 10 1903 (Build 18362)
- [ ] Windows 10 1909 (Build 18363)
- [ ] Windows 10 2004 (Build 19041)
- [ ] Windows 10 20H2 (Build 19042)
- [ ] Windows 10 21H1 (Build 19043)
- [ ] Windows 11 21H2 (Build 22000)
- [ ] Other (specify)
Other Windows Build number
No response
App minimum and target SDK version
- [X] Windows 10, version 1809 (Build 17763)
- [ ] Windows 10, version 1903 (Build 18362)
- [ ] Windows 10, version 1909 (Build 18363)
- [ ] Windows 10, version 2004 (Build 19041)
- [ ] Other (specify)
Other SDK version
No response
Visual Studio Version
2022
Visual Studio Build Number
17.3
Device form factor
No response
Nuget packages
7.1.2
Additional context
Related code: https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/main/Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPicker.xaml#L156-L181 https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/main/Microsoft.Toolkit.Uwp.UI/Converters/ColorToDisplayNameConverter.cs
Help us help you
Yes, I'd like to be assigned to work on this item.
Hello HavenDV, thank you for opening an issue with us!
I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌
@HavenDV I think you're saying there may be an issue with the initialization/converter being used in the template of the control?
Do you know if this is happening in UWP as well or just WinUI 3? It'd be great if we could define a test case in our unit tests which can replicate this issue and then find a solution.
I'll assign to you to take a look at. If we can find a solution in the next week/week-and-a-half, we should be able to include in our upcoming hotfix release.
(As for the PDB Debugging issue see https://github.com/dotnet/sdk/issues/1458)
Yes, the problem manifested itself in both WinUI and UWP. SampleApp also shows this issue:
As I understand it, I can add validation directly to existing test:
https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/main/UITests/UITests.Tests.Shared/Controls/ColorPickerButtonTest.cs
The only problem is that ~~I do not understand how I can find out if there are Binding Failures at the moment~~ a debugger must be attached to an application to be able to send a binding failure via DebugSettings.BindingFailed += (_, args) => Log.Error($"Binding failed: {args.Message}");
https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.debugsettings.bindingfailed?view=winrt-22621
@HavenDV the Page
part of the UITests happen at the app level, so you could have the page register to the event and change some element on the page based on success/failure. Then in the test file side just look for that element to have changed after invoking the change in the test to see the result as a simple form of communication between the two sides.
For the sample app, that's just running the sample app locally with the existing sample (no changes) to exhibit the issue, eh?
Yes, the problem is present in the sample application, you only need to have a debugger attached to see it. The same goes for tests. The DebugSettings.BindingFailed event won't fire without a debugger attached to the app (not tests), which is why I didn't add it to the PR
I confirmed this issue in the SampleApp.
As stated, it is caused by the two-way binding of a nullable GridView.SelectedValue
with the non-nullable ColorPicker.Color
property. Going from ColorPicker -> GridView is fine but the convert back when there is no selection will attempt to set null to the Color
property.
This isn't a critical issue but the binding failure shouldn't happen.