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.

Home Page:https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`PropertyChanged` triggered on empty string

karmeye opened this issue · 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
  • 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

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

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.

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?