简体   繁体   中英

Windows Phone: Data binding does not update the value in the interface

I have a Textblock item in my MainPage which binds the value of a myClass object. I also have a button which changes the value of a property of this object. Although updating the value when clicking the button and implementing the INotifyPropertyChanged Interface the represented value does not change. Here is my code :

public class myClass : INotifyPropertyChanged
{
    //Fields declaration <---------------------------------------------------->

    private int lifetime;
    private DateTime startingDate;
    private string brand;
    private double power;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public int Lifetime {
        get
        {
            return lifetime;
        }
        set
        {
            if (value != lifetime)
            {
                lifetime = value;
                NotifyPropertyChanged("Lifetime");
            }

        }
    }

    public DateTime StartingDate {
        get
        {
            return startingDate;
        }
        set
        {
            if (value != startingDate)
            {
                startingDate = value;
                NotifyPropertyChanged("StartingDate");
            }
        }
    }

    public string Brand
    {
        get
        {
            return brand;
        }
        set
        {
            if (value != brand)
            {
                brand = value;
                NotifyPropertyChanged("Brand");
            }
        }
    }

    public double Power
    {
        get
        {
            return power;
        }
        set
        {
            if (value != power)
            {
                power = value;
                NotifyPropertyChanged("Power");
            }
        }
    }

    public int DaysRemaining
    {
        get
        {
            return Lifetime - (DateTime.Now - StartingDate).Days;
        }
    }

    //Functions declaration <------------------------------------------------>

    public ContactLens()
    {
        StartingDate = new DateTime();
    }


} 

And the button function which changes the startingDate value, and as a result should change the DaysRemaining value too.

    private void leftButtonChange_Click(object sender, RoutedEventArgs e)
    {
        Model.Left.StartingDate = DateTime.Now;


    }

    private void rightChangeButton_Click(object sender, RoutedEventArgs e)
    {
        Model.Right.StartingDate = DateTime.Now;

    }

EDIT: I created a method which updates the date and computes again the DaysRemaining but still although the textBlock binded to the StartingDate changes value the DaysRemaining value demands a restart of the app to make the changes:

private void leftButtonChange_Click(object sender, RoutedEventArgs e) { Model.Left.Replace();
}

    private void rightChangeButton_Click(object sender, RoutedEventArgs e)
    {
        Model.Right.Replace();
    }

And the main class function:

   public void Replace()
    {
        MessageBox.Show("" + daysRemaining);
        StartingDate = DateTime.Now;
        UpdateDaysRemaining();
        MessageBox.Show("" + daysRemaining);

    }

Your StartingDate property fires a change notification so that anything bound directly to it will update, but the DaysRemaining property does not fire a change notification. DaysRemaining needs to fire a change notification to let objects bound to it know they need to update.

DaysRemaining is a bit of an odd property since it is computed from three other values rather than being set explicitly.

Tying DaysRemaining to StartingDate and Lifetime is pretty easy: they are closely related in the same class and StartingDate and Lifetime change just based on their setters. You can have StartingDate and Lifetime fire a change notification for DaysRemaining when they both change, StartingDate and Lifetime could set DaysRemaining so DaysRemaining can fire its own notification, or DaysRemaining could listen for StartingDate's and Lifetime's change notifications (this last one's probably overkill).

The trickier part is that DaysRemaining will change with DateTime.Now. DateTime.Now is external to the class and will change much more frequently than you care about. You only care when (DateTime.Now - StartingDate).Days changes, and that's predictable. Instead of binding to DateTime.Now I'd calculate when the Days will change and set a timer to update DaysRemaining at the change.

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