本文介绍了Update复选框,并更改相应的列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现以下目标:在 IsDeleteBtnShow 属性



功能> MyViewModel 是显示一个删除图标,如果任何项目的复选框是真实的。然而,由于访问复选框财产 ListViewModel ,我需要从 ListViewModel 通过此更新 MyViewModel



不过,我凑了 ListItemViewModel 空例外。但是,当我打电话 ListItemViewModel 构造不为空。我不知道我做错了?



MyViewModel.cs



 公共类MyViewModel:MvxViewModel 
{

私人的ObservableCollection< ListItemViewModel> _myListViews;

公众的ObservableCollection< ListItemViewModel> MyListViews
{
{返回_myListViews; }

{
_myListViews =价值;
RaisePropertyChanged(()=> MyListViews);
}
}

私人布尔_isDeleteBtnShow = FALSE;
公共BOOL IsDeleteBtnShow
{
获得{
返回_isDeleteBtnShow;
}
集合{
_isDeleteBtnShow =价值;
RaisePropertyChanged(()=> IsDeleteBtnShow);
}
}

公共无效初始化(字符串MYID)
{
名单,LT; ListItemViewModel> allListItems =新的List< ListItemViewModel>();
allListItems = _myService.GetAllItems(身份识别码);

的foreach(在allListItems VAR MYTEST)
{
_myListViews.Add(ListItemViewModel.CreateViewModel(MYTEST));
}
ListItemViewModel的obj =新ListItemViewModel(本);

}
}



ListItemViewModel.cs

 公共类ListItemViewModel 
{
公众诠释标识{搞定;组; }
公共字符串文本{搞定;组; }
私人布尔_isChecked;
公众的DateTime日期{搞定;组; }

只读MyViewModel _parent;

公共ListItemViewModel()
{
//参数的构造函数
}

公共ListItemViewModel(MyViewModel父)
$ { b $ b // _parent不为null这里
_parent =父母;
}

公共静态ListItemViewModel CreateViewModel(测试实体)
{
如果(实体== NULL)
{
返回NULL;
}
返回新ListItemViewModel
{
日期= entity.Date,
IsSelected = entity.IsSelected,
};
}

公众的ICommand CheckBoxSelectionCommand
{
得到
{
返回新MvxCommand(()=>
{
VAR =器isChecked IsSelected;
//下面_parent为null
_parent.IsDeleteBtnShow = TRUE;
});
}
}

私人布尔_isSelected;

公共BOOL IsSelected
{
{返回_isSelected; }

{
_isSelected =价值;
RaisePropertyChanged(()=> IsSelected);
}
}

公共BOOL器isChecked
{
{返回_isChecked; }

{
_isChecked =价值;
}
}
}


解决方案

由于您使用 ListItemViewModel 你所得到的异常,当你调用 CreateViewModel 以创建视图模型。



您需要 MyViewModel 参数添加到它,并通过视图模型创建 ListItemViewModel

 列表< ListItemViewModel> allListItems =新的List< ListItemViewModel>(); 
allListItems = _myService.GetAllItems(身份识别码);

的foreach(在allListItems VAR MYTEST)
{
_myListViews.Add(ListItemViewModel.CreateViewModel(这一点,MYTEST));
}



ListItemViewModel

 公共静态ListItemViewModel CreateViewModel(MyViewModel父母,测试实体)
{
如果(实体== NULL)
{
返回NULL;
}
返回新ListItemViewModel(父)
{
日期= entity.Date,
IsSelected = entity.IsSelected,
};
}


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;
        }
    } 
}
解决方案

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

这篇关于Update复选框,并更改相应的列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 16:04