How to link ItemClick in MvxListView in MvxListView

In my example below, I want to bind the ItemClick command to an item in an MvxListView. Here I have in my ViewModel a list of faces that contains a list of dogs.

Binding ItemsSource HasDogs works great.

When MvvmCross tries to associate an ItemClick SelectDogCommand with an ICommand in the Viewmodel, I get this exception.

[0:] MvxBind:Warning: 11,30 Unable to bind: source property source not found Property:SelectDogCommand on Person [0:] MvxBind:Warning: 11,30 Unable to bind: source property source not found Property:SelectDogCommand on Person 12-04 15:05:03.062 I/mono-stdout(16338): MvxBind:Warning: 11,30 Unable to bind: source property source not found Property:SelectDogCommand on Person 

Hope you can help.

Here is my example:

 public class FirstViewModel:MvxViewModel { private List<Person> _persons; public List<Person> Persons { get { return _persons; } set { _persons = value; } } private Cirrious.MvvmCross.ViewModels.MvxCommand<Dog> _selectDog; public System.Windows.Input.ICommand SelectDogCommand { get { _selectDog = _selectDog ?? new Cirrious.MvvmCross.ViewModels.MvxCommand<Dog>(SelectDog); return _selectDog; } } private void SelectDog(Dog item) { ShowViewModel<DetailViewModel>(new DetailViewModel.Parameters{dog = item}); } } 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; } } } public class Dog{...} 

Android View Xml:

FirstView:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/apk/res-auto" ...> <TextView ... local:MvxBind="Text Persons" <Mvx.MvxListView ... local:MvxBind="ItemsSource Persons" local:MvxItemTemplate="@layout/item_person" /> </LinearLayout> 

item_person:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/apk/res-auto" ... android:layout_height="200dp"> <TextView ... local:MvxBind="Text Name" /> <Mvx.MvxListView ... local:MvxBind="ItemsSource HasDogs; ItemClick SelectDogCommand" local:MvxItemTemplate="@layout/item_dog" /> </LinearLayout> 
+6
source share
1 answer

The DataContext element for your list of faces is Person - so your SelectDogCommand should be part of the Person class - for example, 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 can force Person to inherit MvxNavigatingObject (or MvxPropertyChanged or MvxViewModel) - in this case ShowViewModel methods will also be available.

+6
source

Source: https://habr.com/ru/post/959328/


All Articles