0

I have an entry that binds to a int? property in my view model. The problem is that if at a first time I have a value and I delete it, the notification is not arise to the view model.

I would like that if the user delete the value of the entry, to be notifiy to the view model.

This is my code:

View:

<Entry Text="{Binding MyIntProperty}" Keyboard="Numeric"
       HorizontalTextAlignment="End"
       Grid.Column="1"/>

The ViewModel:

[ObservableProperty]
int? _myIntProperty;

partial void OnMyIntPropertyChanged(int? value)
{
    //Do Something if it is null.
}

I am using CommunityToolkit to handle the notification. When the value of MyIntProperty changes, the code in OnMyIntpropertyChanged() is executed.

The problem is that when user delete the data in the entry control, I guess it is null and then it is not notifify the change. I would like to can do something if the value changes to null.

Thanks.

Julian
  • 5,290
  • 1
  • 17
  • 40
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • 1
    Subscribe to the classes PropertyChanged event – Jason Jun 03 '23 at 12:29
  • Please clarify the problem and explain what you're trying to accomplish. Do you want to intercept the `null` value before it gets set? – Julian Jun 03 '23 at 12:48
  • @Jason The event is from the Entry control in the view. And is the view model who subcribes, handle it with the mehtod OnMyIntPropertyChanged(). This method is execute when the the value in the entry changes to any value that is not null. If it is null, it is not notify. – Álvaro García Jun 03 '23 at 19:03
  • @Julian What I want is that if in the entry the value is null, get null in the method of the view model that handle the propery changed event of the entry control, the method OnMyPropertyChanged(). This method is executed when the value in the entry is not null, but not when it is null. I would like to recive the null value too, when it changes from a not null value to null value. – Álvaro García Jun 03 '23 at 19:05
  • The ViewModel shouldn't know about the View and it shouldn't subscribe to events from the View. Please show all the relevant bits and pieces of your code. There may be a different way to solve this, if you show more code and explain what your actual goal is. Why are you doing this? What's the purpose? – Julian Jun 03 '23 at 19:10
  • Instead of using [ObservableProperty] attribute, manually implement it using a [BindableProperty](https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/bindable-properties). On that you can [Detect property changes](https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/bindable-properties#detect-property-changes), and do whatever you want. If the BindableProperty’s propertyChanged doesn’t get called, then set a breakpoint in the corresponding property setter. See if that is set with value null. If neither of those works, then the entry is not sending the null value to the binding… – ToolmakerSteve Jun 03 '23 at 22:28
  • … An alternative is to use Entry’s TextChanged event, and detect empty string. OR write a custom handler for Entry, to change its behavior. – ToolmakerSteve Jun 03 '23 at 22:28

1 Answers1

0

I would:

  1. Use NullableIntConverter to do it. (There are examples on xamarin forms somewhere on this site)

Or:

  1. Make 2nd property that is notified when the first property changes. https://learn.microsoft.com/en-us/dotnet/api/microsoft.toolkit.mvvm.componentmodel.alsonotifychangeforattribute?view=win-comm-toolkit-dotnet-7.1

Or:

  1. Not make it nullable and run it as normal int. (After all 0 is not really that bad of an answer for empty string, we have been doing this for years before "the modern" ways)

And there are always Validators, that can make you UI respond to empty entries.

Technically, you are asking for 1. You can do 2 twice as fast. And I would recommend going 3 instead of dealing with nullables.

Your choice.

H.A.H.
  • 2,104
  • 1
  • 8
  • 21