如何使用MVVM绑定MonoTouch TableView中的删除按钮

时间:2013-02-05 05:59:02

标签: ios mvvm xamarin.ios mvvmcross

我需要将Mono touch的Table View中出现的删除按钮绑定到我的ViewModel中的命令吗?

1 个答案:

答案 0 :(得分:1)

一种方法是更改​​MyItemType(在ViewModel的集合中使用),使其具有PleaseDeleteMeCommand,然后将其称为:

        public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
        {
            if (editingStyle == UITableViewCellEditingStyle.Delete)
            {
                var item = (MyItemType)GetItemAt(indexPath);
                item.PleaseDeleteMeCommand.Execute(null);
            }
            base.CommitEditingStyle(tableView, editingStyle, indexPath);
        }

另一种方法是将命令添加到拥有的ViewModel中。

        public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
        {
            if (editingStyle == UITableViewCellEditingStyle.Delete)
            {
                var item = (MyItemType)GetItemAt(indexPath);
                viewModel.PleaseDeleteItemCommand.Execute(item);
            }
            base.CommitEditingStyle(tableView, editingStyle, indexPath);
        }

如果愿意,可以采用两种方式来使用声明性数据绑定 - 只需将相关的ViewModel端ICommand绑定到客户端属性。


显然,您也可以使用自定义按钮而不是内置表删除按钮来实现相同的功能 - 请参阅视频http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html了解宠物店如何销售小猫(涉及删除行)。