Problems with complex view model binding and OnPropertyChanged call

I'm trying to work with Xamarin Forms and XAML and I have a view which requires a relativly complex view model. The following example represents my current view model:

class MyContext : INotifyPropertyChanged {
    public BasicInfo Info { get; }
    public ObservableCollection<MyFirstListItem> firstListItems { get; }
    public ObservablleCollection <MySecondListItem> secondListItems { get; }

    public MyContext() {
        this.BasicInfo = new BasicInfo { Name = "The Machine!", Duration = 10 };
        this.firstListItems = new ObservableCollection<MyFirstListItem>(new[] {
            new MyFirstListItem { Part = 1, Topic = "Drinking" },
            new MyFirstListItem { Part = 2, Topic = "Waking Up" }
        this.secondListItems = new ObservableCollection<MySecondListItem>(new[] {
            new MySecondListItem{ Title = "Some title", Duration: 120 },
            new MySecondListItem{ Title = "Some other title", Duration: 90 }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName]string propertyName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

The classes BasicInfo, MyFirstListItem and MySecondListItem are simple classes with getter/setter properties of primitive types.

In my page class I set the BindingContext to an instance of MyContext . And in the XAML part I can display all of the children without a problem. But when a child (e.g. the Title of one of the MySecondListItem instances) changes it is not changed in the GUI.
This resulted in several observations and questions:

  1. I'm pretty sure I need to call OnPropertyChanged whenever I change something. So it should be done in the setter methods. I got this from the Xamarin documentation. I've adapted the classes to call the OnPropertyChanged on the instance of the MyContext class. This didn't change anything. To make it clear: I called the OnPropertyChanged of the MyContext class in the setter of the Title property in the MySecondListItem class.
  2. What's the default way to bind complex classes? Is there a way to do mulitple bindings on the different elements of the GUI? Or is there only one BindingContext and I need to handle the OnPropertyChanged differently?

Any help on this would be greatly appreciated.

Best Answer


Sign In or Register to comment.