Reputation: 77
I'm stuck on how to bind the MvxBindableTableViewCell's accessory to a boolean.
I have the table's ItemsSource bound to a list in my ViewModel, showing a nice list of clickable items.
However I want the cell's accessory (UITableViewCellAccessory.Checkmark) to show only when this object is flagged. By flagged I mean a boolean in the model is set to true.
Does anyone know how to bind the cell's accessory?
EDIT: I can show the Accessory depending on the model's boolean, but it's not bound.
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier);
if (cell == null)
cell = new PlotsTableViewCell(UITableViewCellStyle.Subtitle, CellIdentifier);
Plot p = (Plot)item;
if (p.Done)
cell.Accessory = UITableViewCellAccessory.Checkmark;
return cell;
}
Upvotes: 3
Views: 468
Reputation: 66882
I think you can probably do this in your PlotsTableViewCell.
If you declare a custom cell, then you can bind within that cell at runtime.
You can see an example of this in: https://github.com/slodge/MvvmCross/blob/vnext/Sample%20-%20CirriousConference/Cirrious.Conference.UI.Touch/Cells/SessionCell2.cs which is used in the sessions display:
You can see that the cell provides public properties like:
public string RoomText
{
get { return Label2.Text; }
set { if (Label2 != null) Label2.Text = value; }
}
and then provides binding text like:
'RoomText':{'Path':'Item.Session','Converter':'SessionSmallDetails','ConverterParameter':'SmallDetailsFormat'},
For binding an Accessory to a Bool, you should be able to do something like:
public bool IsDone
{
get { return Accessory == UITableViewCellAccessory.Checkmark; }
set
{
if (value)
{
Accessory = UITableViewCellAccessory.Checkmark;
}
else
{
Accessory = UITableViewCellAccessory.None;
}
}
}
with text:
'IsDone':{'Path':'Done'},
As an advanced technique, you could also use a custom image inside a custom drawn button instead of an Accessory in your cell. To see how to do this, take a look at how the IsFavorite
property is bound in that conference sample - see the two way custom bindings in https://github.com/slodge/MvvmCross/tree/vnext/Sample%20-%20CirriousConference/Cirrious.Conference.UI.Touch/Bindings
Upvotes: 3