My binded properties dosent work on change

Carlo Goretti 121 Reputation points
2021-08-02T20:36:15.767+00:00

Hey,

I have some properties who are binded in xaml code and when i change some values of those properties then it dosent sync over to the xaml code...
I guess that i miss something but cant find what...
Any suggestions?

Here is my XAML Code:
<StackLayout Orientation="Horizontal">
<ImageButton Source="Left_Arrow.png"
x:Name="PreviousButton"
HorizontalOptions="StartAndExpand"
Command="{Binding BackOneDayCommand}"
BackgroundColor="Transparent">
</ImageButton>
<Label x:Name="CurrentDayLbl"
FontSize="28"
HorizontalOptions="Center"
Text="{Binding CurrentDay}">
</Label>
<ImageButton Source="Right_Arrow.png"
Command="{Binding ForwardOneDayCommand}"
HorizontalOptions="EndAndExpand"
BackgroundColor="Transparent">
</ImageButton>
</StackLayout>

And here is my C# Code:
public string CurrentDay { get; set; }
public Command BackOneDayCommand { get; }
public Command ForwardOneDayCommand { get; }
public bool WorkoutsListVisible { get; set; }
public bool NoTrainingLblVisible { get; set; }

        public ObservableCollection<Workouts> WorkoutsListSource { get; set; }

        public TodaysWorkoutViewModel()
        {
            CurrentSelectedWeekDay = DateTime.Today;


            CurrentDay = CurrentSelectedWeekDay.DayOfWeek.ToString();

            BackOneDayCommand = new Command(PreviousButton_Clicked);
            ForwardOneDayCommand = new Command(NextButton_Clicked);
        }

        public void PreviousButton_Clicked()
        {
            CurrentSelectedWeekDay = CurrentSelectedWeekDay.AddDays(-1);
            CurrentDay = CurrentSelectedWeekDay.DayOfWeek.ToString();
        }
        public void NextButton_Clicked()
        {
            CurrentSelectedWeekDay = CurrentSelectedWeekDay.AddDays(+1);
            CurrentDay = CurrentSelectedWeekDay.DayOfWeek.ToString();
        }

Thankful for some help!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-08-03T01:41:03.49+00:00

    Hello @Carlo Goretti ,​

    Welcome to our Microsoft Q&A platform!

    In order to update the view at runtime, the model class should implement INotifyPropertyChanged interface to raise the PropertyChanged event when a property is changed. In your project, please make the 'CurrentDay' parameter to raise the PropertyChanged event in the model class.

    Check the code:

       public class CustomModel : INotifyPropertyChanged  
       {  
           private string currentDay;  
           public string CurrentDay  
           {  
               get  
               {  
                   return currentDay;  
               }  
               set  
               {  
                   if (currentDay != value)  
                   {  
                       currentDay = value;  
                       NotifyPropertyChanged();  
                   }  
               }  
           }  
         
           ...  
         
           protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")  
           {  
               PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
           }  
         
           public event PropertyChangedEventHandler PropertyChanged;  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,181 Reputation points
    2021-08-02T20:56:50.073+00:00

    It seems that you don't implement INotifyPropertyChangend

    0 comments No comments