Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind ItemClick in MvxListView in MvxListView

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 of Dog.

The ItemsSource HasDogs binding works fine.

When MvvmCross is trying to bind ItemClick SelectDogCommand to the 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{...}

The 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>
like image 566
aleklass Avatar asked Dec 04 '13 14:12

aleklass


1 Answers

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.

like image 51
Stuart Avatar answered Jan 02 '23 08:01

Stuart