Both TouchBehavior.CommandParameter and TouchBehavior.LongPressCommandParameter are always passed as null to Command and LongPressCommand.
In the following example, when an item in the CollectionView is tapped, the resulting alert will always display Selected value: because the CommandParameter is always null. It should say Selected vale: Item 1, for example, when the user taps Item 1.
<VerticalStackLayout Padding="30,0"
Spacing="25">
<Label Text="{Binding Title}"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />
<Label Text="Tapping or long pressing an item does not correctly pass that item as CommandParameter or LongTouchCommandParameter."
Style="{StaticResource SubHeadline}"
SemanticProperties.HeadingLevel="Level1" />
<CollectionView x:Name="collectionView"
ItemsSource="{Binding Items}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="{x:Type x:String}">
<Label Padding="10"
Text="{Binding .}"
x:Name="Label">
<Label.Behaviors>
<toolkit:TouchBehavior Command="{Binding Source={x:Reference collectionView}, Path=BindingContext.ShowItemCommand}"
CommandParameter="{Binding .}"
LongPressCommand="{Binding Source={x:Reference collectionView}, Path=BindingContext.ShowItemCommand}"
LongPressCommandParameter="{Binding .}"
<!--
CommandParameter and LongPressCommandParameter are always passed as null in the CommunityToolkit.Maui TouchBehavior.
this was not the case with https://github.com/Axemasta/Maui.TouchEffect -->
</Label.Behaviors>
</Label>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</VerticalStackLayout>
public partial class MainViewModel : ObservableObject
{
public string Title => "Hello, Maui!";
public ObservableCollection<string> Items { get; set; } = ["Item 1", "Item 2", "Item 3"];
/// <summary>
/// item is always null. This was not the case in https://github.com/Axemasta/Maui.TouchEffect
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
[RelayCommand]
private Task ShowItem(string item) => Application.Current.MainPage.DisplayAlert($"Selected value:", item, "OK");
}
Sample Reproduction
Here is a link to a reproduction sample for this bug: https://github.com/hansmbakker/bugrepro-communitytoolkit-maui-touchbehavior-parameter

Explanation
In .NET MAUI, you are required to manually set the
BindingContextfor everyBehavior. In other words, Behaviors do not inherit theBindingContextof theVisualElementto which they are attached.Source: https://learn.microsoft.com/dotnet/maui/fundamentals/behaviors?view=net-maui-8.0#create-a-net-maui-behavior
Updated Code
Here is the updated XAML to get your code working. It uses a view-to-view binding to bind
TouchBehavior.BindingContexttoLabel.BindingContext:A bit unrelated to the Behavior BindingContext problem, here is my recommendation for small performance improvement to your ViewModel (both
TitleandItemscan be read-only properties):Screenshot of Updated App
Here is a screen shot of your app working as expected after updating the code:
.NET MAUI Community Toolkit Unit Tests
Here are the Unit Tests I wrote for the
TouchBehaviorinCommunityToolkit.Mauiwhere we verify theLongPressCommanddoes indeed pass inLongPressCommandParameter;https://github.com/CommunityToolkit/Maui/blob/51dc63a7642370849e413d87072b668fa022e303/src/CommunityToolkit.Maui.UnitTests/Behaviors/TouchBehaviorTests.cs#L612-L674
And here are the Unit Tests I wrote for the
TouchBehaviorinCommunityToolkit.Mauiwhere we verify theCommanddoes indeed pass inCommandParameter;https://github.com/CommunityToolkit/Maui/blob/51dc63a7642370849e413d87072b668fa022e303/src/CommunityToolkit.Maui.UnitTests/Behaviors/TouchBehaviorTests.cs#L550-L610