c# - Problem in maui Community toolkit longpress - Stack Overflow

admin2025-04-27  3

I Have a problem using the community toolkit gesture recognizer for the longpress. I use a collection view of an object and for an easy to read use I add the item in this collection in reverse mode using : MyClassCollection.Insert(0, NewItem);

The collection come out without problem...

This is the code that I use:

<CollectionView Margin="10"
                x:Name="RigheCocoCollection"
                ItemsSource="{Binding ArticoliCollection}"
                SelectionMode="Single"
                SelectedItem="{Binding ArticoloPerModifica}"
                BackgroundColor="DarkCyan"
                HeightRequest="320">
                
    <CollectionView.Header>
        <VerticalStackLayout>
            <Label HeightRequest="25" VerticalTextAlignment="Center" HorizontalOptions="Center" Text="Righe Copia Commissione" FontAttributes="Bold" FontSize="15" TextColor="White"/>
        </VerticalStackLayout>
    </CollectionView.Header>
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="ModelCoco:RigaCoco">
                <SwipeView>
                    <SwipeView.RightItems>
                        <SwipeItem Text="Test"
                                   BackgroundColor="Red" CommandParameter="{Binding .}"
                                   Command="{Binding BindingContext.TestCommand, Source={x:Reference this}}"/>
                    </SwipeView.RightItems>
                    
                    <SwipeView.LeftItems>
                        <SwipeItem Text="Test 2"
                                   BackgroundColor="Red"/>
                                   
                    </SwipeView.LeftItems>
                    
                    <Border BackgroundColor="AntiqueWhite" Padding="5" StrokeThickness="2" Stroke="Black">
                    
                        <Grid ColumnDefinitions="Auto,*,*"
                              RowDefinitions="20,32,20"
                              ColumnSpacing="5">

                              <Label Text="{Binding cod_articolo}" Grid.ColumnSpan="3"/>
                              <Label Grid.Row="1" Text="{Binding descr_1}" Grid.ColumnSpan="3" FontSize="13"/>
                              <Label Grid.Row="2" Text="{Binding Unita_misura}"/>
                              <Label Grid.Row="2" Grid.Column="1" Text="{Binding quantita}"/>
                              <Label Grid.Row="2" Grid.Column="2" Text="{Binding Prezzo, StringFormat='€ {0:F4}'}"/>
                              
                              <Grid.Behaviors>
                                    <Toolkit:TouchBehavior LongPressCommand="{Binding BindingContext.DeleteRowCommand, Source={x:Reference this}}"
                                                               LongPressCommandParameter="{Binding .}" 
                                                               LongPressDuration="1000"/>
                              </Grid.Behaviors>
                        </Grid>
                    </Border>
                </SwipeView>
            </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

If I add some line on the collection view (3-4 or more), with the left and right gesture the collection view return the correct line, If I use the long press it return in this way:

  • The first line returen the first
  • The second line return the third
  • The third line return the forth
  • .....
  • The last but one return the last
  • The last return the last.

If I insert the item in the normal way also the longpress gesture work without problem.

Someone can Help me?

转载请注明原文地址:http://anycun.com/QandA/1745707781a91109.html