Nested List in MVVM

TeymurAsadovTeymurAsadov USMember ✭✭

Hi everyone, I'm trying to implement some logic inside my MVVM:
I have one model with List property:
public class Exercise { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public List<Group> Groups { get; set; } }
Also I have one intermediate level class:
` public class SelectableData : INotifyPropertyChanged
{
private T _Data;
public T Data
{
get { return _Data; }
set { _Data = value; NotifyPropertyChanged("Data"); }
}

    private bool _Selected;
    public bool Selected
    {
        get { return _Selected; }
        set {
            _Selected = value;
            NotifyPropertyChanged("Selected"); }
    }

    #region INotifyPropertyChanged      
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
}`

How it can be implemented in MVVM, and get notified when any of the switches is toggled ?

Tagged:

Answers

  • TeymurAsadovTeymurAsadov USMember ✭✭

    here is screenshot what is already done

  • JarvanJarvan Member, Xamarin Team Xamurai

    How it can be implemented in MVVM, and get notified when any of the switches is toggled ?

    The View and the ViewModel are often connected through data bindings defined in the XAML file. Set data binding to contorl and detect the event to update data.

    Tutorial about Data Bindings to MVVM, you can refer to.
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm

  • TeymurAsadovTeymurAsadov USMember ✭✭

    Thank you for your response. I know how it works with properties but haven't found anything for List. The set method is not being called when any of the switches is toggled
    ObservableCollectionSelectableData<Group> _groups; public ObservableCollection<SelectableData<Group>> Groups { get => _exercise.Groups; set { _exercise.Groups = value; NotifyPropertyChanged("Groups"); } }

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited August 30

    You created the List in Exercise class, but not introduce the Exercise class in SelectableData class. The logic isn't like mvvm.
    Move public List<Group> Groups { get; set; } } or introduce the Exercise class to SelectableData class.

    And please post more details about the code or share a basic demo to reproduce the issue.

Sign In or Register to comment.