Search code examples
c#xamarinmvvmcross

Update CheckBox and change corresponding listitem


I want to achieve the following:

Functionality of the IsDeleteBtnShow property in the MyViewModel is to display a delete icon if any of the item's checkbox is true. However, since property of checkbox accessible in the ListViewModel, I need to pass this update from ListViewModel to MyViewModel.

However I am getting null exception in the ListItemViewModel. But when I call ListItemViewModel constructor parent is not null. I wonder what I am doing wrong?

MyViewModel.cs

 public class MyViewModel:MvxViewModel
 {

    private ObservableCollection<ListItemViewModel> _myListViews;

    public ObservableCollection<ListItemViewModel> MyListViews
    {
        get { return _myListViews; }
        set
        {
            _myListViews= value;
            RaisePropertyChanged(() => MyListViews);
        }
    }

    private bool _isDeleteBtnShow = false;
    public bool IsDeleteBtnShow
    {
        get {
            return _isDeleteBtnShow;
        }
        set {
            _isDeleteBtnShow = value;
            RaisePropertyChanged(() => IsDeleteBtnShow);
        }
    }

    public void Init(string myId)
    {
         List<ListItemViewModel> allListItems = new List<ListItemViewModel>();
         allListItems = _myService.GetAllItems(myId);

         foreach (var myTest in allListItems)
         {
           _myListViews.Add(ListItemViewModel.CreateViewModel(myTest));
         }
        ListItemViewModel obj = new ListItemViewModel(this);

      }
   }

ListItemViewModel.cs

public class ListItemViewModel
{
    public int Id { get; set; }
    public string Text { get; set; }
    private bool _isChecked;
    public DateTime Date { get; set; }

    readonly MyViewModel _parent;

    public ListItemViewModel()
    {
        // parameterless constructor
    }

    public ListItemViewModel(MyViewModel parent)
    {
      // _parent is not null here
        _parent = parent;
    }

   public static ListItemViewModel CreateViewModel(Test entity)
    {
        if (entity == null)
        {
            return null;
        }
        return new ListItemViewModel
        {
            Date = entity.Date,
            IsSelected = entity.IsSelected,
         };
    }

     public ICommand CheckBoxSelectionCommand
    {
        get
        {
            return new MvxCommand(() =>
            {
                var isChecked = IsSelected;
                // the following _parent is null
                _parent.IsDeleteBtnShow = true;
            });
        }
    }

    private bool _isSelected;

    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            _isSelected = value;
            RaisePropertyChanged(() => IsSelected);
        }
    }

    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;
        }
    } 
}

Solution

  • You are getting the exception because you use parameterless constructor of ListItemViewModel when you call CreateViewModel to create the view model.

    You need to add MyViewModel parameter to it and pass the view model when creating ListItemViewModel:

         List<ListItemViewModel> allListItems = new List<ListItemViewModel>();
         allListItems = _myService.GetAllItems(myId);
    
         foreach (var myTest in allListItems)
         {
              _myListViews.Add(ListItemViewModel.CreateViewModel(this, myTest));
         }
    

    and in ListItemViewModel:

        public static ListItemViewModel CreateViewModel(MyViewModel parent, Test entity)
        {
            if (entity == null)
            {
                return null;
            }
            return new ListItemViewModel(parent)
            {
                Date = entity.Date,
                IsSelected = entity.IsSelected,
            };
        }