Forum Xamarin.Forms

Binding button command parameter inside DataTemplate

Umar3xUmar3x FRMember ✭✭✭
edited November 2017 in Xamarin.Forms

Hello all,

I found a lot of example in XAML but can't figure out how to manage it in pure c# : Like here =>

What I want to translate is this sentence of @LoriLalonde

7) Bind the Button's Command property to the StartTimerCommand in your ViewModel. Make sure to set its Command Source to the BindingContext used by the ListView which will point to the ViewModel. > And bind the Button's CommandParameter to the current list item's Timer object:

I want to pass as commandParameter my customObject that is used as DataContext for the datatemplate or at least one of it's property (the ID).
I got a listView for which the itemSource is a customObject.
I got a dataTemplate in which I got a button and some others controls bound to properties, something like that :

var dateResponse = new MR.Gestures.Label();
                dateResponse.SetBinding(Label.TextProperty, "RespondedDate",
                    converter: new ScheduleResponseConverter());

     acceptScheduleButton.SetBinding(MR.Gestures.Button.TappedCommandProperty, "ToLastStepTradeCommand");
                acceptScheduleButton.TappedCommandParameter = ??;
                acceptScheduleButton.BindingContext = _thirdStepTradeViewModel;

How can I pass as commandParameter the DataContext of the dataTemplate ? Someting like in XAML => {Binding}


Best Answers


  • Umar3xUmar3x FRMember ✭✭✭
    edited November 2017

    Hello @Charwaka thanks for your answer but as you read I want example in pure c# ..

    acceptScheduleButton.SetBinding(MR.Gestures.Button.TappedCommandProperty, "ToLastStepTradeCommand");
     acceptScheduleButton.TappedCommandParameter = ??;
    acceptScheduleButton.BindingContext = _thirdStepTradeViewModel;

    This snipet is in the DataTemplate, I just want to know what I have to pass to my TappedCommandParameter ?
    My acceptScheduleButton has the same bindingContext as the page (the thirdStepTradeViewModel) and not same as controls in my dataTemplate like DateResponse which is bound to the object.

    if you want to do backend on click event cast the sender object to your model and pass like this
    I dont understand what you mean. My button has not the same binding context as controls in dataTemplate as I'm binding to a viewModel which holds the command "ToLastStepTradeCommand" and what I want is just one property of the object that is bound to my listView DataTemplate to be a parameter of my command =>
    thus I can for example get the ID of the item that is inside this viewCell for which the button was clicked (as I dont click the listview but the button inside of it)

  • CharwakaCharwaka INMember ✭✭✭✭✭

    Hi @Umar3x

    Ok by using the ID filter you collection to find the whole object from viewmodel.
    Here list is Your Collection which your binding to list

    var result = _thirdStepTradeViewModel.list.Find(x => x.Id == "yourID"); 
  • Umar3xUmar3x FRMember ✭✭✭
    Accepted Answer

    @Charwaka thanks for your help but I finally achieved what I wanted, you helped me find clues =>

    acceptScheduleButton.SetBinding(MR.Gestures.Button.TappedCommandParameterProperty, new Binding("."));
    acceptScheduleButton.SetBinding(MR.Gestures.Button.TappedCommandProperty, new Binding("BindingContext.ToLastStepTradeCommand", source: this));

    Thanks to this thread :

  • Umar3xUmar3x FRMember ✭✭✭
    edited November 2017

    One last question which is linked with this topic :

    What's the equivalent c# of
    {Binding Source = {x:refrence SchedulePage}}

Sign In or Register to comment.