Binding button command parameter inside DataTemplate

Umar3xUmar3x FRMember ✭✭
edited November 11 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 => https://forums.xamarin.com/discussion/47751/binding-to-call-a-method-of-the-binding-class-from-a-button

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}

Thanks

Best Answer

Answers

  • CharwakaCharwaka INMember ✭✭✭
    edited November 11

    Hi @Umar3x

    Here is best example for what you want to acheive

    https://blog.xamarin.com/simplifying-events-with-commanding/

    if you want to do backend on click event cast the sender object to your model and pass like this

    _thirdStepTradeViewModel.StartTimerCommand.Execute(obj);

    Please Mark as Answer if this works

  • Umar3xUmar3x FRMember ✭✭
    edited November 11

    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"); 
    
    _thirdStepTradeViewModel.StartTimerCommand.Execute(result);
    
  • 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 : https://forums.xamarin.com/discussion/61317/calling-command-from-viewmodel-using-button-within-listview

  • Umar3xUmar3x FRMember ✭✭
    edited November 11

    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.