Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

How can I do TwoWay data binding with dependent properties?

$
0
0

I have a model class that has two properties Status and DateShipped.
When Status is set to Shipped, I set DateShipped to today's date.
When Status is set to something other than Shipped, I set DateShipped to null to indicate that the date is not defined.

public DateTime? DateShipped
{
    get
    {
        return ContainsKey(DATE_KEY) ? (DateTime?) DateTime.Parse(this[DATE_KEY].AsString()).Date : null;
    }
    private set
    {
        if (value == null)
            RemoveKey(DATE_KEY);
        else
            this[DATE_KEY] = new PrimitiveBusinessValue(value.Value.ToString("yyyy-MM-dd"));
        NotifyPropertyChanged("DateShipped");
    }
}

public string Status
{
    get { return this[STATUS_KEY].AsString(); }
    set
    {
        this[STATUS_KEY] = new PrimitiveBusinessValue(value);
        if (Status == ManifestStatus.Shipped)
        {
            if (!ContainsKey(DATE_KEY))
                // Should trigger update of View for DateShipped!
                DateShipped = DateTime.Today.Date;
        }
        else
        {
            if (DateShipped != null)
                // Should trigger update of View for DateShipped!
                DateShipped = null;
        }
        NotifyPropertyChanged("Status");
    }
}

Both of these properties are bound to View objects using two way binding.
The two way binding works OK except that when the status is changed via the UI, the View corresponding to DateShipped does not get updated.

It seems that two way binding can't handle nested updates. I would call that a bug.

Is there any non-hacked workaround for this problem?

It seems like whenever I do something that is not totally trivial with Xamarin Forms, it breaks. Is it just me?


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>