I have an ItemCell
which inherits from MvxTableViewCell
. Below is simplified:
Constructor:
public ItemCell(IntPtr handle) : base(handle)
{
CreateLayout();
InitializeBindings();
}
CreateLayout()
creates and constrains, among other elements:
UILabel
_label
, andUIButton
_button
InitializeBindings:
private void InitializeBindings()
{
this.DelayBind(() =>
{
var set = this.CreateBindingSet<ItemCell, ItemViewModel>();
set.Bind(_label).For(x => x.Text).SourceDescribed("'Label: ' + ItemNumber");
set.Bind(_button).To(vm => vm.ItemCommand);
set.Apply();
});
}
ViewModel contains the following property and command:
private string _itemNumber;
public string ItemNumber
{
get { return _itemNumber; }
set { SetProperty(ref _itemNumber, value); }
}
private IMvxCommand _itemCommand;
public IMvxCommand ItemCommand
{
get
{
return _itemCommand?? (_itemCommand= new MvxCommand(() => {
//Logic
}));
}
}
When the TableView
is bound to a collection and the cells are repeated, _label
's text renders the correct value ("Label: {ItemNumber}"), but clicking the button doesn't hit the ItemCommand
's get
. I have also tried adding .For("TouchUpInside")
to the button's binding, but that didn't change anything.
I'm confused as to why the label binds correctly, but the button does not.
Unfortunately I do not have enough points to add a comment so I'll add my suggestion as an answer.
What possibly might be happening is that your touch event is handled for the whole cell and therefore the event is not passed down to the children of that cell. Your button might be bound correctly but since your button within your cell will never receive the touch event the command which you bound to will never get fired.
EDIT
You can have a look at this link which seems to address your problem. https://forums.xamarin.com/discussion/15560/how-to-add-a-custom-button-in-a-table-cell