Hello,
but I was wondering what is the best practice if I wanted to dynamically set the Name property in the view model (using the value of a XAML control)?
If you get the Monkey object at the runtime in the viewmodel, then you want to update this object at the runtime without create a new Object. you can do this by achieving INotifyPropertyChanged
interface for your Monkey.cs
.
public class Monkey: INotifyPropertyChanged
{
private string name;
public string Name
{
set
{
if (name != value)
{
name = value;
OnPropertyChanged("Name");
}
}
get
{
return name;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Then you can set the Name
directly with monkey.Name="Mooch"
and send it
Here is document about Part 5. From Data Bindings to MVVM - Xamarin | Microsoft Learn
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.