How to bind ItemClick in MvxListView in MvxListView

前端 未结 1 651
滥情空心
滥情空心 2021-01-18 03:48

In my example below I want to bind an ItemClick Command to the Item in the MvxListView. Here I have in my ViewModel a List of Person that contains a List o

相关标签:
1条回答
  • 2021-01-18 04:08

    The DataContext for your person list item is a Person - so your SelectDogCommand needs to be part of the Person class - e.g. something like:

    public class Person
    {
        private string _name;
        private List<Dog> _hasDogs;
    
        public List<Dog> HasDogs
        {
          get { return _hasDogs; }
          set { _hasDogs = value; }
        }
    
        public string Name
        {
          get { return _name; }
          set { _name = value; }
        }
    
        private Cirrious.MvvmCross.ViewModels.MvxCommand<Dog> _selectDog;
        public System.Windows.Input.ICommand SelectDogCommand
        {
            get
            {
                _selectDog = _selectDog ?? new Cirrious.MvvmCross.ViewModels.MvxCommand<Dog>(dog => _parent.SelectDog(dog));
                return _selectDog;
            }
        }
    
        private FirstViewModel _parent;
        public Person(FirstViewModel parent)
        {
            _parent = parent;
        }
    }
    

    or alternatively you could get Person to inherit from MvxNavigatingObject (or MvxPropertyChanged or MvxViewModel) - in which case the ShowViewModel methods will be available there too.

    0 讨论(0)
提交回复
热议问题