dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

MVVM binding doen't work properly for int types #8342

Open jballa1 opened 2 years ago

jballa1 commented 2 years ago

Description

When we bind int property. It works fine as long as there is at least one digit. If we remove value by back space. Even though there are no digits, it shows the first digit of last entered value

Steps to Reproduce

Create Maui APP Add View model with one int property Add Entry and a lable in XML file Bind the property to lable and entry enter any number ex: 456 Press back space and remove all the digits Bug: The entry shows no digits as expected but the property holds first digit (ex: 4 in the previous number) and it displays the same in the label

Version with bug

6.0 Release Candidate 2 or older

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows, I was not able test on other platforms

Affected platform versions

All

Did you find any workaround?

No

Relevant log output

No response

jfversluis commented 2 years ago

Could you maybe add a little more detail in this area

Bind the property to lable and entry

How do you bind it? Could you please add some relevant code or ideally create a little reproduction project and put that on GitHub that we can take a look at?

ghost commented 2 years ago

Hi @jballa1. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

jballa1 commented 2 years ago

BindingTest.zip I am attaching test project reproducing the issue

jballa1 commented 2 years ago

image You can run earlier repo and try these test cases

jballa1 commented 2 years ago

Here is the repo in git https://github.com/jballa1/Maui-Binding-Test

ghost commented 2 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

jfversluis commented 1 year ago

This is still happening. Under the hood a FormatException is thrown in the BindingExpression.TryConvert(ref object value, BindableProperty targetProperty, Type convertTo, bool toTarget) (at the time of writing, here). Even when using a nullable int. The problem is that an empty string is coming in when the textbox becomes empty.

A workaround is to use it as a string and do the conversion to and from that to an int yourself.

XamlTest commented 1 year ago

Verified this on Visual Studio Enterprise 17.7.0 Preview 1.0. Repro on Windows 11 and Android 13.0 with below Project: 8342.zip

Issue1: Press Backspace and remove all numbers, it shows first number. Issue2: Select all the number and delete by delete key, the number remains same. MVVMbinding