CommunityToolkit / CommunityToolkit/MVVM-Samples

How to work with nullable properties?

Open
#118 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
No language data
Stars
1.4k
Forks
265
PR merge metrics
No merged PRs in 30d

Description

I have a Textbox that binds to a long? property in the view model.

I would like that if the TextBox is empty, to set the value of the property to null. But when the TextBox is empty, the property in the view model is not changed, it keeps the last digit that the TextBox had.

I would like to have null when the TextBox is empty.

I was trying to intercept the prperty changed event in this way:

```
[ObservableProperty]
long? _myProperty;

partial void OnMyPropertyChanging(long? value)
{
Console.WriteLine($"Name has changed to {value}");
_myproperty = value;
}
```

However the behaviour is not very good. When I write this secuence in the TextBox I get this behaviour:

1.- Write 5. I get 5 as value.
2.- Delete 5, the method is not execute
3.- Write 5, the method is not execute
4.- Write 5, the value of the method is 55

How could I bind to a long? property?

Thanks.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.