简体   繁体   中英

Emptying an Entry does not reset bound property

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM