OiO.lk English Android .NET Maui Android TouchBehaviour MCT Bug with Binding
Android

.NET Maui Android TouchBehaviour MCT Bug with Binding


I’m working on a .NET MAUI page with a CollectionView bound to a list of items. Each item in the collection has a Button, defined in a DataTemplate, and I’m using the TouchBehavior from the .NET MAUI Community Toolkit to bind Command and CommandParameter for click events.

Here’s the code snippet that demonstrates the issue:

<Button Text="GoTo">
    <Button.Behaviors>
        <toolkit:TouchBehavior 
            Command="{Binding Source={x:Reference MyView}, Path=BindingContext.ItemClickedCommand}"
            CommandParameter="{Binding .}"
            LongPressCommand="{Binding Source={x:Reference MyView}, Path=BindingContext.ItemLongClickedCommand}"
            LongPressCommandParameter="{Binding .}"
            LongPressDuration="250" />
    </Button.Behaviors>
</Button>

The CommandParameter is bound to the current item in the CollectionView. When I refresh the data (e.g., reload the bound list), and click the button, the CommandParameter shows an incorrect ID—it differs from the expected item ID in the refreshed list, even though the list order and item positions remain the same.

Strangely, if I use a standard Command on the Button (like below), the issue doesn’t occur, and the correct CommandParameter is passed even after the data refresh:

<Button Text="GoTo" 
    Command="{Binding Source={x:Reference MyView}, Path=BindingContext.ItemClickedCommand}" 
    CommandParameter="{Binding .}" />

Question: Why does the TouchBehavior’s CommandParameter not reflect the correct item after refreshing the data in the CollectionView? I want to detect if the button is clicked with a short or long press, which isn’t possible with the default button. Is there a workaround to ensure that the correct item ID is passed after a data refresh while still detecting short and long presses?



You need to sign in to view this answers

Exit mobile version