Pass and grab the Commandparameter from a Xamarin Forms button

Asked

Viewed 2,305 times

2

How to pass a value that is in one Binding for CommandParameter of a button that is inside a ListView to the ViewModel ?

The button is accessing the Command but I don’t know if this is how the CommandParameter nor how to take its value in the ViewModel.

How can I do that ?

My code:

\\ XAML

<ListView x:Name="lvEnderecos" RowHeight="205">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
            <Label x:Name="lblCEP" Font="14" TextColor="Black" Text="{Binding CliEndCep, StringFormat='CEP: {0}'}"></Label>
              <Grid x:Name="GridControl3" RowSpacing="0" ColumnSpacing="0">
                <Grid.RowDefinitions>
                  <RowDefinition Height="25"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <StackLayout x:Name="stkManipularEndereco" Grid.Row="0" Grid.Column="0" Padding="5, 0, 0, 0" HeightRequest="25" WidthRequest="25">
                  <Button x:Name="btnEditarEndereco" BackgroundColor="Transparent" Image="editar2.png" HeightRequest="25" WidthRequest="25" BindingContext="{Binding Source={x:Reference lvEnderecos}, Path=BindingContext}" Command="{Binding EditarEnderecoCommand}" CommandParameter="{Binding CliEndCep}" />
                </StackLayout>
              </Grid>
            </StackLayout>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

\\ Viewmodel

    this.EditarEnderecoCommand = new Command(async () =>
    {
        try
        {
             page.DisplayAlert("Alerta", "Você clicou aqui :)", "OK");
        }                
        catch (Exception ex)
        {
             throw ex;
        }
    }
});
  • I recently posted a post about the command implementation, I use Command<> being the Generic argument what will be received. Take a look here, I hope it helps you. https://guidedgrammerss.wordpress.com/2016/12/mvvm-e-seus-commands/

  • I think it’s possible to do as @Filipesantiago showed, but using the Binding class on its own, you’d just have to take the value from Binding, with a Command<System.Windows.Data.Binding> :D I can’t make an example at the moment :(

  • In Xamarin you don’t even need to implement Command<>, it already has native library. It makes it easier to just add the type parameters.

  • It has not been solved here:http://answall.com/questions/173501/bot%C3%A3o-command-n%C3%A3o-funciona-dentro-da-listview-Xamarin-Forms-mvvm/173634#173634 ?

  • Not @rubStackOverflow, only the Command event issue. Missing this part of passing and grabbing the Commandparameter value.

1 answer

3

I found the solution to my problem, I added the name pro ContentPage which in this case contain my ListView and the other components.

Thus:

x:Name="MeusEnderecosView"

In my Command I added the BindingContext referencing the name of this ContentPage.

Thus:

Command="{Binding Path=BindingContext.ExcluirEnderecoCommand, Source={x:Reference MeusEnderecosView}}"

And the CommandParameter was like this:

CommandParameter="{Binding .}"

And in the ViewModel, to take the value of some Binding View just do it that way:

this.ExcluirEnderecoCommand = new Command<Endereco>(async (model) => 
{
     Endereco objEnd = new Endereco();
     objEnd.EndCep = model.EndCep;
     objEnd.EndCodigo = model.EndCodigo;
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.