C# 无法绑定到mvvmcross中的ios表节单元格,子单元格按预期绑定
我正在尝试创建一个包含分组分区的TableView,如下所示: 我正在将组列表绑定到表中。组包含一些字符串属性和项目列表,而项目也包含一些字符串属性。组单元格和项目单元格都有自己的类,扩展了C# 无法绑定到mvvmcross中的ios表节单元格,子单元格按预期绑定,c#,ios,xamarin,mvvmcross,C#,Ios,Xamarin,Mvvmcross,我正在尝试创建一个包含分组分区的TableView,如下所示: 我正在将组列表绑定到表中。组包含一些字符串属性和项目列表,而项目也包含一些字符串属性。组单元格和项目单元格都有自己的类,扩展了MvxTableViewCell 在类中,我绑定的每个单元格如下所示: 项目单元格: this.DelayBind(() => { var set = this.CreateBindingSet<ItemCellView,
MvxTableViewCell
在类中,我绑定的每个单元格如下所示:
项目单元格:
this.DelayBind(() =>
{
var set = this.CreateBindingSet<ItemCellView, ItemCellViewModel>();
set.Bind(lblOne).To(item=> item.propertyOne);
set.Apply();
});
我认为您的解决方案就快到了-但是您需要提供一个
GetViewForHeader
实现,它有点像MvvmCross提供的绑定GetCell
实现-注意下面的DataContext
设置:
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var item = GetItemAt(indexPath);
var cell = GetOrCreateCellFor(tableView, indexPath, item);
var bindable = cell as IMvxDataConsumer;
if (bindable != null)
bindable.DataContext = item;
return cell;
}
从
对于标题,您需要将
DataContext
设置为组标题的适当“视图模型”。您也可以发布您的TableViewSource吗?@Cheesebaron刚刚修改了帖子并添加了它谢谢,这成功了。我很好奇为什么必须为标题行而不是常规行手动设置数据上下文?
private class TableSource : MvxStandardTableViewSource
{
private List<GroupCellViewModel> _groups;
public new List<GroupCellViewModel> ItemsSource
{
get
{
return _groups;
}
set
{
_groups = value;
ReloadTableData();
}
}
public TableSource(UITableView tableView)
: base(tableView)
{
}
public override string TitleForHeader(UITableView tableView, int group)
{
if (_groups == null)
return string.Empty;
return Utils.FormatDate(_groups[group].Date);
}
public override UIView GetViewForHeader(UITableView tableView, int group)
{
return MakeHeaderRow(tableView, _groups[group]);
}
public override float GetHeightForHeader(UITableView tableView, int section)
{
return 50;
}
public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
{
return 50;
}
public override int NumberOfSections(UITableView tableView)
{
if (_groups == null)
return 0;
return _groups.Count;
}
public override int RowsInSection(UITableView tableview, int group)
{
if (_groups == null)
return 0;
return _groups[group].Items.Count;
}
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
UITableViewCell cell;
if (item is Item)
{
cell = MakeItemRow(tableView, (Item)item);
}
else
{
throw new ArgumentException("Unknown cell type " + item.GetType().Name);
}
return cell;
}
protected override object GetItemAt(NSIndexPath indexPath)
{
if (_groups == null)
return null;
return _groups[indexPath.Section].Items[indexPath.Row];
}
private UITableViewCell MakeHeaderRow(UITableView tableView, GroupCellViewModel group)
{
var existing = (GroupCellView)tableView.DequeueReusableCell(GroupCellView.Key);
if (existing != null)
return existing;
return new GroupCellView();
}
private UITableViewCell MakeItemRow(UITableView tableView, Item item)
{
var existing = (ItemCellView)tableView.DequeueReusableCell(ItemCellView.Key);
if (existing != null)
return existing;
return new ItemCellView();
}
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var item = GetItemAt(indexPath);
var cell = GetOrCreateCellFor(tableView, indexPath, item);
var bindable = cell as IMvxDataConsumer;
if (bindable != null)
bindable.DataContext = item;
return cell;
}