CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.08k stars 300 forks source link

`PropertyChanged` triggered on empty string #872

Open karmeye opened 7 months ago

karmeye commented 7 months ago

Describe the bug

The following returns false even though both are empty strings

if (!global::System.Collections.Generic.EqualityComparer<string?>.Default.Equals(myString, value))

Regression

No response

Steps to reproduce

1. <TextBox Text="{x:Bind MyString, Mode=TwoWay}" />
2. Leave empty and tab in and out.
3. `OnPropertyChanged` (and the other methods) is called

Expected behavior

PropertyChanged should not be triggered when the new value is an empty string for a current empty string value.

Screenshots

No response

IDE and version

VS 2022

IDE version

No response

Nuget packages

Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

No, just wanted to report this

jhm-ciberman commented 7 months ago

I copypasted the line you mentioned in a REPL console, and it works as expected. Are you sure you are not triggering something else that is causing the OnPropertyChanged to being called?

image

karmeye commented 6 months ago

Ok, what happens is that the first time I tab out of the text box, Text (i.e. the binding) is null and this triggers PropertyChanged, as null and empty string is not considered equal. Just by tabbing in and out of the text box, the textbox changes the binding value from null to empty string. After that OnPropertyChanged is not called any more.

I guess we can remove the bug label.

karmeye commented 6 months ago

I don't really want the TextBox to change the value, as it is represented as null rather than empty string in the database.

Is there a way to conditionally prevent a control from updating the binding?