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/
– Filipe Santiago
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 :(– Johnny Santos
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.
– Filipe Santiago
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 ?
– rubStackOverflow
Not @rubStackOverflow, only the Command event issue. Missing this part of passing and grabbing the Commandparameter value.
– AndreeH