0
votes

I have a Xamarin.Forms application. It is using freshmvvm framework, which is not relevant for my issue. A page has a couple of Entries:

        <Entry Placeholder="Width" Text="{Binding TileWidth, Mode=TwoWay}" />
        <Entry Placeholder="Height" Text="{Binding TileHeight, Mode=TwoWay}" />

These entries are bound to the ViewModel properties:

    int _tileWidth;
    public int TileWidth
    {
        get => _tileWidth;
        set
        {
            _tileWidth = value;
            RaisePropertyChanged(nameof(TileWidth));
        }
    }

    int _tileHeight;
    public int TileHeight
    {
        get => _tileHeight;
        set
        {
            _tileHeight = value;
            RaisePropertyChanged(nameof(TileHeight));
        }
    }

If the user enters some numbers into the entries, binding sets the properties accordingly. But if after that the user DELETES a value from an entry, binding does not reset the corresponding property to 0, which causes various issues. The execution doesn't even come to the property's set part. (If the user explicitly enters 0, the bound property is set to 0 as expected).

Maybe somebody has an idea what I am missing? Thanks.

1
Entry.Text is a string while you are binding to int, maybe that is the problem. Try to use a converter or int?. - EvZ
@EvZ int? breaks the validation, but using string as the property type works. Please make you suggestion an answer, and I will mark it. Thank you for your help. - David Shochet

1 Answers

1
votes

Entry.Text expects a string while you are binding it to int.
In order to solve the problem you may:

  1. Use an IValueConverter if you want to keep bindings to int properties (TileWidth, TileHeight).
  2. Simply bind it to string properties. Not a good idea since we are talking about properties representing a Width & Height.