CollectionView nested into a ListView

I've tried to nest a CollectionView inside a ListView.... I was able to visualize all what i needed but now I cannot reach the click event on a single cell of a single CollectionView inside the list. This is my xaml code:

<ListView  x:Name="MainListView" VerticalOptions="FillAndExpand">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <CollectionView Margin="10" SelectionMode="Single" ItemsSource="{Binding singleList}" FlowDirection="LeftToRight" ItemsLayout="HorizontalList">
                            <CollectionView.ItemTemplate>
                                <DataTemplate>
                                    <Grid Padding="10" Margin="10">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <Frame BackgroundColor="White" Grid.RowSpan="2" CornerRadius="5" Padding="10">
                                            <Image Source="{Binding ImageName}" Aspect="AspectFill" HeightRequest="100" WidthRequest="100" />
                                        </Frame>
                                        <Label Grid.Column="1" Text="{Binding Name}" FontAttributes="Bold" />
                                        <Label Grid.Row="1" Grid.Column="1" Text="{Binding Date}" VerticalOptions="End" />
                                        <Image Grid.Row="2" Grid.Column="0" Source="shop" HeightRequest="20" WidthRequest="20" />
                                    </Grid>
                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

I suppose I have to use the SelectionChangedCommand event inside the Collection, but I don't understand... how!! Some help?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 10

    t now I cannot reach the click event on a single cell of a single CollectionView inside the list.

    You mean the click event on Grid or the Labels or the entire ViewCell ?

    I think you could add TapGestureRecognizer on the layout or control you want to reach .

    For example

                <DataTemplate>
                     <Grid Padding="10" Margin="10">
                        <Grid.GestureRecognizers>
                            <TapGestureRecognizer/>
                        </Grid.GestureRecognizers>
                     </Grid>
              </DataTemplate>
    
  • GiulianoCandrevaGiulianoCandreva ITMember ✭✭

    Thank for the reply, but the problem is that command function not firing...
    I've added this:

    But the breakpoint in code behind don't stop the execution.... the event does not start!!!

  • ColeXColeX Member, Xamarin Team Xamurai

    Maybe there is conflict between with ListView.ItemSelected event and your custom event .

    Try to disable the selected event in a listview ,refer https://stackoverflow.com/a/46275735/8187800.

    <ListView ItemSelected="ListView_ItemSelected" />
    ...
    private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
          var list = (ListView)sender;
          list.SelectedItem = null;
    }
    
Sign In or Register to comment.