Button Command is not working in the Xamarin forms

CKuttan 21 Reputation points
2020-11-23T09:48:35.113+00:00

In the Xamarin wpf project, when clicking on the button, command is not working.
I have a button in the datatemplate of a listview item template and a command has been binded to this button. This is as shown below :

<ListView x:Name="lstView" ItemsSource="{Binding Foods}" HorizontalOptions="End" RowHeight="150">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="2">
<Grid.RowDefinitions>
<RowDefinition Height="" />
<RowDefinition Height="
" />
</Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>  
                            <ColumnDefinition Width="100"/>  
                            <ColumnDefinition Width="*"/>  
                        </Grid.ColumnDefinitions>  

                        <Image Source="{Binding FoodImage}" Grid.Column="0" Grid.RowSpan="3" HeightRequest="100"   
                                                     WidthRequest="100" />  

                        <Label Text = "{Binding FoodName}" Grid.Column="1" Grid.Row="0" FontSize="15" />  
                          

                        <StackLayout Orientation="Horizontal" Grid.Column="1" Grid.Row="1">  
                            <Label Text = "{Binding Price}" FontSize="15" WidthRequest="60" />  
                            <Editor Text="{Binding Quantity}" Keyboard="Numeric" Placeholder="Quantity" MaxLength="3"   
                                                   HeightRequest="30" IsSpellCheckEnabled="False"  
                                                   WidthRequest="50" Margin="2"  IsTextPredictionEnabled="False" />  

                            <Button Text="+ to Cart" Command="{Binding AddToCartCommand}"  HeightRequest="30" WidthRequest="100"   
                                          BackgroundColor="Black" TextColor="White" />  
                            <Button Text="X" Command="{Binding OrderClearCommand}" HeightRequest="30" WidthRequest="40"   
                                           BackgroundColor="Black" TextColor="White" />  
                        </StackLayout>                              

                    </Grid>  
                </ViewCell>  
            </DataTemplate>  
        </ListView.ItemTemplate>  
    </ListView>  

And in my viewmodel class, i have the code for this :

    public ICommand AddToCartCommand  
    {  
        get;  

}

And in the constructor of this viewmodel class, i have the code like this :

   AddToCartCommand = new Command(AllCalculate);  

----------------------------------------------

void AllCalculate()
{
Prices = 100;
Application.Current.MainPage.DisplayAlert("Total", "Hai", "ok");
}

Why the button command is not triggering when button is clicked ?
I am using VS2019.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Morgan SOULLEZ 1 Reputation point
    2020-11-25T13:03:29.637+00:00

    Your Command is in your ViewModel, so the BIndingContext in the ViewCell is the Item of your ItemsSource (Foods property). To bind to a property of your ViewModel use the Source keyword in your Binding to make a reference to your Page, and access to your Command by the BindingContext of the page (assuming that your page BindingContext is your ViewModel), example :

    <ContentPage ...  
                            x:Name="page">  
    [..ListView..DataTemplate..ViewCell...]  
    <Button Text="Add" Command="{Binding BindingContext.AddToCartCommand, Source={x:Reference page}}" />  
    [...]  
    </ContentPage>  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.