Need some help on MAUI binding in collection view

85 views Asked by At

I need some help on binding in a TapGestureRecognizer. I have the following code where I create a CollectionView. I create a TapGestureRecognizer in that view and would like the command parameter to the object in the list itself. I can't seem to figure out a way to word the binding in the command parameter to make this happen. I worked around it for the time being by setting the selection mode and using the bound value of "SelectedObject". Ideally, I would like the commandparameter to be the object in the list itself.

Thanks.

    <CollectionView x:Name="switchCollection"
                    ItemsSource="{Binding ShopSwitches}"
                    ItemsLayout="HorizontalGrid, 4"
                    SelectedItem="{Binding SelectedSwitch,Mode=TwoWay}"
                    SelectionMode="Single"
                    Grid.Column="1"
                    Grid.Row="1">
        <CollectionView.GestureRecognizers>
            <TapGestureRecognizer
                Command="{Binding OnSwitchTappedCommand}"
                CommandParameter="{Binding Source={RelativeSource AncestorType={x:Type BindableObject}}}"
                NumberOfTapsRequired="2"/>
        </CollectionView.GestureRecognizers>
2

There are 2 answers

3
Jason On

use this

CommandParameter="{Binding .}" 

. represents the current object

0
Jianwei Sun - MSFT On

As Jason said that your gesture is attached to the entire view, not the individual rows, take the following code as an example, you can add gesture on Grid:

<ContentPage ....
             xmlns:vm="clr-namespace:MauiApp2.ViewModels">
    
    <ContentPage.BindingContext>
        <vm:viewmodels/>
    </ContentPage.BindingContext>
    
    <VerticalStackLayout>
        <CollectionView ItemsSource="{Binding Habits}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Grid Padding="10">
                        <Grid.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type vm:viewmodels}}, Path=TapCommand}"
                                                  CommandParameter="{Binding .}"/>
                        </Grid.GestureRecognizers>
                        
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        
                        <Label Grid.Column="1"
                               Text="{Binding Name}"/>
                        <Label Grid.Row="1"
                               Grid.Column="1"
                               Text="{Binding ID}"/>
                    </Grid>
                    
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
   </VerticalStackLayout>
</ContentPage>

ViewModel:

public partial class viewmodels
{
    ....
    
    public Command TapCommand { get; }
    
    public viewmodels () 
    { 
        TapCommand = new Command<object> (OnTap); 
        
        ....
    }

    private void OnTap (object obj)
    {
        var h = obj as Habit;
        Console.WriteLine (h.Name);
    }
    
    ....
}

When you tap the item of collectionview, console will print Name of the item. And here is the effect.