How to get which viewcell the user clicked on, and pick up a text from a label that is in this same Cell view via commandParameter?

Asked

Viewed 30 times

0

I have the following code in my view

<ListView x:Name="MyListView"
          SeparatorVisibility="None"
          HasUnevenRows="true"
          ItemsSource="{Binding Items}"
          ItemTapped="Handle_ItemTapped"
          CachingStrategy="RecycleElement">

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell x:Name="cell">
                <StackLayout x:Name="item" Orientation="Horizontal" Margin="0,0,0,5" HeightRequest="110" BackgroundColor="White">
                    <BoxView WidthRequest="8" BackgroundColor="#381F72"/> 
                    <Image Source="{Binding img}"/>
                    <StackLayout HorizontalOptions="FillAndExpand">
                        <Label Text="{Binding nome}" 
                               FontSize="18" 
                               FontAttributes="Bold"
                               TextColor="#381F72"
                               HorizontalOptions="CenterAndExpand"/>
                        <Label Text="{Binding QtdServicos}" 
                               FontSize="14"
                               TextColor="#381F72"
                               HorizontalOptions="CenterAndExpand"/>
                        <StackLayout Orientation="Horizontal" Margin="0,15,0,0">
                            <StackLayout x:Name="Item" Orientation="Horizontal" HorizontalOptions="Start">
                                <Image Source="{Binding ok}"/>
                                <Label Text="{Binding Avaliacoes}"
                                       FontSize="14"
                                       TextColor="#03FD9B"
                                       VerticalOptions="End"/>
                            </StackLayout>
                            <Button Text="CONTRATAR"
                                    Padding="5"
                                    Margin="0,0,10,0"
                                    BackgroundColor="#03FD9B"
                                    TextColor="White"
                                    HeightRequest="20"
                                    FontSize="10"
                                    CornerRadius="5"
                                    BindingContext="{Binding Source={x:Reference MyListView}, Path=BindingContext}"
                                    Command="{Binding Path=BindingContext.ContratarCommand, Source={x:Reference ContactView}}"
                                    CommandParameter="{Binding}"
                                    HorizontalOptions="EndAndExpand"/>
                        </StackLayout>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>

</ListView>

and my model view..

 private Command<object> _ContratarCommand;
    public Command<object> ContratarCommand => _ContratarCommand ?? (_ContratarCommand = new Command<object>(ContratarCommandExecute));


    async void ContratarCommandExecute(object value)
    {
        try
        {
            // Aqui o VALUE vem a lista contendo todos os viewCell na tela com as suas respectivas propriedades.

            await NavigationService.Current.NavigateTo(new ChatView(...,...));
        }
        catch (Exception ex)
        {
            await App.Current.MainPage.DisplayAlert("Entre em contato com nossa equipe de suporte!", ex.Message, "ok");
        }

    }

i wanted to get only the content of viewcell where the button is clicked, and not all items of my class linked to listview in Itemssource="{Binding Items}" What do I need to change in my code? thanks in advance. my first project so forgive me!

  • For those who have some interest, I was able to solve, the problem was that in my view I was referencing in the button bindingContext my list view here in this line, Bindingcontext="{Binding Source={x:Reference Mylistview}, Path=Bindingcontext}" I changed and started referencing viewCell, gave an x:name to it txtCell and referenced it like this Bindingcontext="{Binding Source={x:Reference txtcell}, Path=Bindingcontext}" , and came only the viewCell item and not a list of all the items in the listview

  • Hello, if your question has been solved, please close the question.

No answers

Browser other questions tagged

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