question

CarloGoretti-1185 avatar image
0 Votes"
CarloGoretti-1185 asked JarvanZhang-MSFT commented

My binded properties dosent work on change

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!

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello @CarloGoretti-1185 ,​

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JarvanZhang-MSFT Hey, Thanks for the help.
My properties are in the ViewModel class... Do i must inherit from the INotifyPropertyChanged class or can i just use the method and call it? :)
Best regards

0 Votes 0 ·

Do i must inherit from the INotifyPropertyChanged class or can i just use the method and call it?

@CarloGoretti-1185
If you want to update the view using the binding way, the answer is 'yes'. The related parameter of the mode/viewModel class must raise the PropertyChanged event.

Without using data binding, you could update the data in code behind directly.

<ImageButton 
    Source="Left_Arrow.png"
    x:Name="PreviousButton"
    HorizontalOptions="StartAndExpand"
    Clicked="PreviousButton_Clicked"
    BackgroundColor="Transparent"/>
<Label 
    x:Name="CurrentDayLbl"
    FontSize="28"
    HorizontalOptions="Center">
<ImageButton 
    Source="Right_Arrow.png"
    Clicked="NextButton_Clicked"
    HorizontalOptions="EndAndExpand"
    BackgroundColor="Transparent"/>

page.xaml.cs

DateTime CurrentSelectedWeekDay = DateTime.Today;
private void PreviousButton_Clicked(object sender, EventArgs e)
{
    CurrentSelectedWeekDay = CurrentSelectedWeekDay.AddDays(-1);
    CurrentDayLbl.Text = CurrentSelectedWeekDay.DayOfWeek.ToString();
}
private void NextButton_Clicked(object sender, EventArgs e)
{
    CurrentSelectedWeekDay = CurrentSelectedWeekDay.AddDays(+1);
    CurrentDayLbl.Text = CurrentSelectedWeekDay.DayOfWeek.ToString();
}
0 Votes 0 ·
alessandrocaliaro avatar image
0 Votes"
alessandrocaliaro answered

It seems that you don't implement INotifyPropertyChangend

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.