How to get Information from ViewCell components for a Tapped event?

Hello together,

I'm new to Xamarin Forms and I did not found a solution for my problem so far.
Right now I create a ObservableCollection to fill my Listview with Data.

The XAML looks like this:

 <ContentPage.Content>
        <ListView x:Name="Test" SeparatorVisibility="Default" IsPullToRefreshEnabled ="True" RefreshCommand="{Binding ReloadCommand}"
          IsRefreshing="{Binding IsBusy, Mode=OneWay}" RowHeight="55" SeparatorColor="Black">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell Tapped="ViewCell_Tapped">
                        <ViewCell.View>
                            <Grid Padding="5">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="20"></RowDefinition>
                                    <RowDefinition Height="20"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>

                                <Label  Grid.Row="0" Grid.Column="0" Text="{Binding ArticleName_Number}" TextColor="Black" LineBreakMode="TailTruncation"></Label>
                                <Label Grid.Row="1" Grid.Column="0" Text="{Binding ProductId}" Font="Small"  TextColor="Gray" LineBreakMode="TailTruncation"></Label>
                                <Label Grid.Row="0" Grid.Column="1" Text="{Binding Status}" TextColor="{Binding Color}" HorizontalTextAlignment="End"/>
                                <Label Grid.Row="1" Grid.Column="1" Text="{Binding ProductionEnd}" TextColor="{Binding Color}" HorizontalTextAlignment="End"/>

                            </Grid>

                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>

And the Constructor:

            public TestPage()
                    {
                        InitializeComponent();
                        ListViewItems = new ObservableCollection<ProductionList> ();
                        BindingContext = this;
                        Test.ItemsSource = ListViewItems;
                        LoadProductionOverview();
                    }

My goal is it, to use the "Tapped="ViewCell_Tapped" to call a constructor for the next NavigationPage with the Value of Label.Text or the "real" Value from the object that was bound to that ViewCell. If i tapp the ViewCell it should call something like:

       await this.Navigation.PushAsync(new Nextpage(1));

Where the 1 should be of course variable depending on the tapped ViewCell.

Summary

I create a Product overview for different Products inside a Listview.
When i tapp one, I want to navigate to a Detailed page for that specific entry.

Question

How do I get the Product Id to the tapped Event?

I hope someone can help me with that (probably totally dumb) question. :smiley:

Best Answer

Answers

  • Norwegian2307Norwegian2307 NOMember

    Thank you so much! That works perfectly fine.
    Is there a documentation for that topic?
    I didn't know about the ItemTappedEventArgs e.
    Well, that's probably due to my lack of experience. :neutral:

Sign In or Register to comment.