Binding command parameter

IanPunchIanPunch USMember ✭✭

Hi, I am having trouble with MVVMCross data binding for an image that appears in my listItem within a listview. My listitem is bound to my OutletInfo class. However, my image is bound to my viewmodel so that I can call methods on the view model. The call to my list item will return same view for performance and its BindingContext changes so different data appears in binding context, but cant seem to propage that to the image. This is probably easier to explain in code

`
public View GetView(Expression<Func<OutletsViewModel, object>> mapToExpression)
{
var set = this.CreateBindingSet<ImageViewWithClickEventRenderer, OutletsViewModel>();

        var newOrderImage = new MvxImageView(_context);
        newOrderImage.SetImageResource(Resource.Drawable.NewOrder);

            //set.Bind(newOrderImage).For("Click").To(mapToExpression).CommandParameter(_parentRenderer.BindingContext.DataContext);
            set.Bind(newOrderImage).For("Click").To(mapToExpression).CommandParameter(new Binding("BindingContext", source: _parentRenderer.BindingContext.DataContext).Source);


        //set.Bind(newOrderImage).For("Click").To(vm => vm.NewOrderClicked).CommandParameter(_listItemBindingContext);

        set.Apply();

        return newOrderImage;
    }`

That is my image renderer code. The view that is created is part of my listitem view. For my listitem view (parentRenderer), when I call GetView I have the following

public View GetView() { if (_currentView != null) { return _currentView; } _newOrderRenderer = new ImageViewWithClickEventRenderer(_context, this); _currentView = _newOrderRenderer; return _currentView;

I have cut out a lot for simplicity. The problem is, when I scroll, the command parameter remains the same as with the original (current) view. So the BindingContext on listitem is updated, but the image exists in my current view and so the command parameter is not changing. What am i doing wrong in terms of binding the command param to my parent list item?

In case this helps, I had it working in forms as follows, but am not using forms for this....

`
var newOrderImage = new Image() { VerticalOptions = LayoutOptions.Center };
newOrderImage.BindingContext = parentListView.BindingContext;
newOrderImage.SetBinding(Image.SourceProperty, "NewOrderIcon");

        var newOrderImageClickGesture = new TapGestureRecognizer();
        newOrderImageClickGesture.SetBinding(TapGestureRecognizer.CommandProperty, "NewOrderClicked");
        newOrderImageClickGesture.SetBinding(TapGestureRecognizer.CommandParameterProperty, new Binding() { Source = this, Path = "BindingContext" });
        newOrderImage.GestureRecognizers.Add(newOrderImageClickGesture);`
Sign In or Register to comment.