dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

`PropertyChanged` triggered on empty string

Open karmeye opened this issue 9 months ago • 3 comments

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

  • [ ] CommunityToolkit.Common
  • [ ] CommunityToolkit.Diagnostics
  • [ ] CommunityToolkit.HighPerformance
  • [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.2.2

Additional context

No response

Help us help you

No, just wanted to report this

karmeye avatar Apr 27 '24 04:04 karmeye

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

jhm-ciberman avatar Apr 30 '24 14:04 jhm-ciberman

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 avatar May 03 '24 03:05 karmeye

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?

karmeye avatar May 03 '24 03:05 karmeye