绑定按钮单击ListView控件模板MvvMCross [英] Binding button click in ListView template MvvMCross
问题描述
我有一个包含按钮模板的列表视图。当按钮获得的点击我要一个触发事件,并返回我的列表视图行的值,这样我就可以用它来把它添加到数据库中。我的问题是,我不知道该怎么对我buttonevent绑定到的ItemTemplate。我已经尝试了一些办法,但没有成功为止。
I have a listview with a template containing a button. When the button get's clicked i want an event to fire and return me a value of the listview row, so i can use it to add it to a database. My problem is, i don't know how to bind my buttonevent to the itemtemplate. I've tried a few approaches but with no success so far.
我的列表视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#aeaeae"
android:dividerHeight="1px"
local:MvxBind="ItemsSource MenuCollection; ItemClick OrderBtnClick"
local:MvxItemTemplate="@layout/listitem_menuitem" />
</LinearLayout>
我的ItemTemplate:
<?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:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
local:MvxBind="ImageUrl ImageUrl" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textSize="40dp"
local:MvxBind="Text Name" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:textSize="20dp"
local:MvxBind="Text ShortDescription" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_alignParentRight="true"
android:layout_marginTop="20dip"
android:layout_marginRight="20dip"
android:layout_gravity="right|center_vertical"
android:text="Bestel"
android:id="@+id/button1" />
</LinearLayout>
</LinearLayout>
我的视图模型:
public class ListPresentationViewModel: MvxViewModel
{
private readonly ISQLService _sqlSvc;
public ListPresentationViewModel (ISQLService sqlService)
{
_sqlSvc = sqlService;
MenuCollection = _sqlSvc.MenuItemGetAll ();
}
private List<MenuItem> _menuCollection = new List<MenuItem> ();
public List<MenuItem> MenuCollection {
get{ return _menuCollection;}
set {
_menuCollection = value;
RaisePropertyChanged (() => MenuCollection);
}
}
private IMvxCommand _orderBtnClick;
public IMvxCommand OrderBtnClick{
get{
_orderBtnClick = _orderBtnClick ?? new MvxCommand(btnClick);
return _orderBtnClick;}
}
private void btnClick()
{
//Do Something
}
}
我把本地:MvxBind =按钮点击OrderBtnClick在模板和列表视图。该项目单击似乎当我删除该ItemTemplate中按钮的工作,但是这不是我要找的。我希望按钮可以触发事件。任何人都可以点我朝着正确的方向?
I placed the local:MvxBind="Click OrderBtnClick" on the button in the template and on the listview. The ItemClick seems to work when i remove the button from the itemtemplate, but that's not what i'm looking for. I want the button to be triggering the event. Can anyone point me in the right direction?
更新的:
UPDATE:
我已经试过了第二个建议,司徒小屋贴<一href="http://stackoverflow.com/questions/12682082/mvvmcross-changing-viewmodel-within-a-mvxbindablelistview">here.这是我的包装类
I've tried the second suggestion stuart lodge posted here. Here is my wrapper class:
public class MenuItemWrap
{
MenuItem _mnuItem;
ListPresentationViewModel _parent;
public MenuItemWrap ()
{
}
public MenuItemWrap (MenuItem item, ListPresentationViewModel parent)
{
_mnuItem = item;
_parent = parent;
}
public IMvxCommand Click {
get {
return new MvxRelayCommand (() => _parent.btnClick(WrapConverter.ConvertToWrapMenuItem(_mnuItem, _parent)));
}
}
public MenuItem Item{ get { return _mnuItem; } }
}
我的视图模型:
public class ListPresentationViewModel: MvxViewModel
{
private readonly ISQLService _sqlSvc;
public ListPresentationViewModel (ISQLService sqlService)
{
_sqlSvc = sqlService;
MenuCollection = WrapConverter.ConvertToWrapperClass(_sqlSvc.MenuItemGetAll (), this);
}
private List<MenuItemWrap> _menuCollection = new List<MenuItemWrap> ();
public List<MenuItemWrap> MenuCollection {
get{ return _menuCollection;}
set {
_menuCollection = value;
RaisePropertyChanged (() => MenuCollection);
}
}
private IMvxCommand _orderBtnClick;
public IMvxCommand OrderBtnClick{
get{
_orderBtnClick = _orderBtnClick ?? new MvxCommand<MenuItemWrap> (btnClick);
return _orderBtnClick;
}
}
public void btnClick(MenuItemWrap item)
{
MenuCollection.Clear ();
}
}
和这里是我的模板
<?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:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
local:MvxBind="ImageUrl Item.ImageUrl" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textSize="40dp"
local:MvxBind="Text Item.Name" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:textSize="20dp"
local:MvxBind="Text Item.ShortDescription" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_alignParentRight="true"
android:layout_marginTop="20dip"
android:layout_marginRight="20dip"
android:layout_gravity="right|center_vertical"
android:text="Bestel"
local:MvxBind="Click btnClick.OrderBtnClick"
android:id="@+id/button1" />
</LinearLayout>
</LinearLayout>
我的列表视图完美的作品。所有属性获得绑定正确,我可以看到这个名字,shortdescription和形象。有什么不工作的按钮点击。在我的应用程序输出我得到一个错误说: MvxBind:警告:76.06无法绑定:源属性源没有发现Cirrious.MvvmCross.Binding.Parse.PropertyPath.PropertyTokens.MvxPropertyNamePropertyToken上MenuItemWrap 的
我已经尝试了几种方法来解决这个问题,但没有成功。我会提到我没有找到在MvvMCross组件的RelayCommand类,所以我副本<一个粘贴的code href="https://github.com/slodge/MonoCrossExtensions/blob/master/Cirrious/Cirrious.MvvmCross/Commands/MvxRelayCommand.cs"相对=nofollow>此处到我的项目。
I've tried a few approaches to fix it, but with no success. I will mention i did not find the RelayCommand class in the MvvMCross assemblies so i copy pasted the code from here into my project.
推荐答案
我已经找到了解决方案。问题是点击绑定。您应该只指在包装类的动作,而不是两个。这里是我的wrapperclass和放大器; ListView控件的ItemTemplate。
I've found the solution. The problem was the click binding. You should only refer to the action in the wrapper class and not both. Here are my wrapperclass & listview itemtemplate.
的ItemTemplate:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_alignParentRight="true"
android:layout_marginTop="20dip"
android:layout_marginRight="20dip"
android:layout_gravity="right|center_vertical"
android:text="Bestel"
local:MvxBind="Click OrderClick" />
</LinearLayout>
WrapperClass:
public class MenuItemWrap
{
MenuItem _mnuItem;
ListPresentationViewModel _parent;
public MenuItemWrap (MenuItem item, ListPresentationViewModel parent)
{
_mnuItem = item;
_parent = parent;
}
public IMvxCommand OrderClick {
get {
return new MvxCommand (() => _parent.btnClick (_mnuItem));
}
}
public MenuItem Item{ get { return _mnuItem; } }
}
我的视图模型:
public class ListPresentationViewModel: MvxViewModel
{
private readonly ISQLService _sqlSvc;
public ListPresentationViewModel (ISQLService sqlService)
{
_sqlSvc = sqlService;
MenuCollection = WrapConverter.ConvertToWrapperClass (_sqlSvc.MenuItemGetAll(), this);
}
private int _catId;
public int CategorieId {
get{ return _catId;}
set{
_catId = value;
ChangeMenuCollection ();
}
}
private void ChangeMenuCollection()
{
MenuCollection = WrapConverter.ConvertToWrapperClass (_sqlSvc.MenuItemByCategorie (_catId), this);
}
private List<MenuItemWrap> _menuCollection = new List<MenuItemWrap> ();
public List<MenuItemWrap> MenuCollection {
get{ return _menuCollection;}
set {
_menuCollection = value;
RaisePropertyChanged (() => MenuCollection);
}
}
private IMvxCommand _orderBtnClick;
public IMvxCommand OrderBtnClick {
get {
_orderBtnClick = _orderBtnClick ?? new MvxCommand<MenuItem> (btnClick);
return _orderBtnClick;
}
}
public void btnClick (MenuItem item)
{
//Do Something
}
}
这篇关于绑定按钮单击ListView控件模板MvvMCross的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!