locked
Nested List in MVVM RRS feed

  • Question

  • User283332 posted

    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 ?

    Thursday, August 29, 2019 11:23 AM

All replies

  • User283332 posted

    here is screenshot what is already done


    Thursday, August 29, 2019 11:26 AM
  • User382871 posted

    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

    Thursday, August 29, 2019 1:15 PM
  • User283332 posted

    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"); } }

    Thursday, August 29, 2019 1:30 PM
  • User382871 posted

    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.

    Friday, August 30, 2019 3:52 PM