Reputation: 1117
Is it possible to nest bound MvxListView's (mvvmcross android v3.1 series)?
I'm finding the nested bind fails with:
MvxBind:Warning: 1.17 Unable to bind: source property source not found Property:Command on MenuSection
Our ViewModel looks a bit like
class SomeViewModel : MvxViewModel{
public List<MenuSection> Sections{get;set;}
}
where
class MenuSection{
public string Title{get;set;}
public MenuItem[] Items{get;set;}
}
class MenuItem{
public string Name {get;set;}
public ICommand Command{get;set;}
}
Cutdown version of the axml with most non-mvx attributes removed looks like:
layout/page_home.axml
<android.support.v4.widget.DrawerLayout>
<FrameLayout android:id="@+id/content_frame"/>
<Mvx.MvxListView
local:MvxBind="ItemsSource Sections"
local:MvxItemTemplate="@layout/item_menusection" />
</android.support.v4.widget.DrawerLayout>
layout/item_menusection.axml
<LinearLayout>
<TextView local:MvxBind="Text Title"/>
<Mvx.MvxListView
local:MvxItemTemplate="@layout/item_menusection_item"
local:MvxBind="ItemSource Items; ItemClick Command" />
</LinearLayout>
layout/item_menusection_item.axml
<TextView local:MvxBind="Text Name"/>
Upvotes: 3
Views: 1034
Reputation: 3143
That's because you are binding the inner ListView to MenuSection object.
The Commmand property is in MenuItem object not in MenuSection.
You need to move the Command to MenuSection.
EDIT:
page_home.axml is bound to SomeViewModel =>
ListView.Items is bound to SomeViewMode.Sections =>
each item defined in item_menusection.axml is bound to a MenuSection =>
(in item_menusection.axml) ListView.Items is bound to MenuSections.Items =>
each item defined in item_menusection_item.axml is bound to MenuItem =>
TextView.Text is bound to MenuItem.Name
Also in item_menusection.axml: ListView.ItemClick is bound to MenuSections.Command (which it doesn't exist)
This is because how you defined layout/item_menusection.axml
<LinearLayout>
<TextView local:MvxBind="Text Title"/>
<Mvx.MvxListView
local:MvxItemTemplate="@layout/item_menusection_item"
local:MvxBind="ItemSource Items; ItemClick Command" />
</LinearLayout>
Both of ListView's ItemSource and ItemClick are bound to the same view-model (MenuSections).
If you want to handle the item click in the MenuItem view-model instead you can do this in few ways:
You can try to make the layout in item_menusection_item clickable and bind the click event to the MenuItem.Command.
Or, add a Command property to MenuSection and just call the Command of the selected MenuItem in turn:
public class MenuSection
{
public string Title{get;set;}
public MenuItem[] Items{get;set;}
public ICommand Command { get { return new MvxCommand<MenuSection>((ms) => ms.Command.Execute(null)); } }
}
Upvotes: 1