Custom ViewCell for binding both Command and CommandParameter?

Hello.

Is this possible in Xamarin Forms? I want to execute a command (passing a command parameter) when a item inside a ListView has been tapped, but I don't want to dirty the code behind...

Posts

  • TorbenKruseTorbenKruse DEMember ✭✭✭
    edited August 2015

    @FranciscoGG I have a subclass of ViewCell implementing this:

    /// <summary>
    /// The bindable property implementation
    /// </summary>
    public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create<CustomViewCell, object>(i => i.CommandParameter, default(object), BindingMode.OneWay);
    /// <summary>
    /// The command parameter
    /// </summary>
    public object CommandParameter
    {
        get
        {
            return this.GetValue(CommandParameterProperty);
        }
        set
        {
            this.SetValue(CommandParameterProperty, value);
        }
    }
    /// <summary>
    /// The bindable property implementation
    /// </summary>
    public static readonly BindableProperty CommandProperty = BindableProperty.Create<CustomViewCell, Command>(i => i.Command, null, BindingMode.OneWay);
    /// <summary>
    /// The command which gets executed on tapping the cell
    /// </summary>
    public Command Command
    {
        get
        {
            return (Command)this.GetValue(CommandProperty);
        }
        set
        {
            this.SetValue(CommandProperty, value);
        }
    }
    
    /// <summary>
    /// Override tapped event
    /// </summary>
    protected override void OnTapped()
    {
        base.OnTapped();
    
        if (this.Command != null)
        {
            this.Command.Execute(this.CommandParameter ?? this);
        }
    }
    
  • stvansolanostvansolano UMInsider, University ✭✭✭

    Hey @FranciscoGG yes it is possible, here is a complete example

    Hope it helps

  • FranciscoGGFranciscoGG ESMember ✭✭

    @TorbenKruse thank you, but how can I deselect the element once it has been tapped?

  • TorbenKruseTorbenKruse DEMember ✭✭✭

    @FranciscoGG Set the SelectedItemto null on the ItemSelectedevent of your ListView

    this.ListView.ItemSelected += (o, e) => { this.ListView.SelectedItem = null; };

  • FranciscoGGFranciscoGG ESMember ✭✭

    Ok @TorbenKruse, but that should be done in CodeBehind or in a custom ListView, right?

  • TorbenKruseTorbenKruse DEMember ✭✭✭

    Yes.

  • FranciscoGGFranciscoGG ESMember ✭✭

    @TorbenKruse, thank you very much!! :)

  • FotalFotal Member ✭✭✭
    edited February 2018

    @NMackay how you add.
    How did you add the behavior to your own cell? What is ?

                      
    </b: BehaviorCollection>
    </b: Interaction.Behaviors>
                      

  • stesvisstesvis USMember ✭✭✭

    @stvansolano said:
    Hey @FranciscoGG yes it is possible, here is a complete example

    Hope it helps

    @stvansolano I tried your example but I always get Command = null in OnTapped(), why would that be?

Sign In or Register to comment.