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);
}