更新CheckBox并更改相应的listitem

时间:2016-04-19 16:55:36

标签: c# xamarin mvvmcross

我想实现以下目标:

IsDeleteBtnShowMyViewModel属性的功能是,如果任何项目的复选框为true,则显示删除图标。但是,由于ListViewModel中可以访问复选框的属性,因此我需要将此更新从ListViewModel传递到MyViewModel

但是我在ListItemViewModel中获得了null异常。但是当我调用ListItemViewModel时,构造函数parent不为空。我不知道我做错了什么?

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

1 个答案:

答案 0 :(得分:2)

您收到异常是因为在调用ListItemViewModel来创建视图模型时使用CreateViewModel的无参数构造函数。

您需要向其添加MyViewModel参数,并在创建ListItemViewModel时传递视图模型:

     List<ListItemViewModel> allListItems = new List<ListItemViewModel>();
     allListItems = _myService.GetAllItems(myId);

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

ListItemViewModel

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