社区工具包放在哪里长按MAUI Collection查看项目

问题描述 投票:0回答:1

我有一个项目的 CollectionView。 我想长按某个项目以使导航调用

LongPressCommand
。 项目的 DataTemplate 是 3 个标签。

    <CollectionView ItemsSource="{Binding FormattedEvents}" 
                    VerticalScrollBarVisibility="Always" 
                    EmptyView="No events recorded."
                    HeightRequest="500" 
                    SelectedItem="{Binding SelectedMatchEvent}"
                    SelectionMode="Single">

        <CollectionView.Behaviors>
            <!--THIS WILL WORK IF I LONG PRESS ON THE EMPTY LIST-->
            <toolkit:TouchBehavior LongPressCommand="{Binding LongPressCommand}"/>
        </CollectionView.Behaviors>

        
        <CollectionView.ItemTemplate>

            <DataTemplate>
                <Grid Padding="10" HorizontalOptions="Center">
                    <Grid.Behaviors>
                        <!--DOESN'T WORK-->
                        <toolkit:TouchBehavior LongPressCommand="{Binding LongPressCommand}"/>
                    </Grid.Behaviors>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="150" />
                        <ColumnDefinition Width="50" />
                        <ColumnDefinition Width="150" />
                    </Grid.ColumnDefinitions>
                    
                    <Label Grid.Row="0" Padding="5,0,5,0"
                                   Grid.Column="0" 
                                   TextColor="White"
                                   Text="{Binding HomeEventMessage}"
                                   FontSize="Medium">
                        <Label.Behaviors>
                            <!--DOESN'T WORK-->
                            <toolkit:TouchBehavior LongPressCommand="{Binding LongPressCommand}"/>
                        </Label.Behaviors>
                    </Label>

                    <Label Padding="5,0,5,0"
                                       Grid.Column="1" 
                                       TextColor="White"
                                       Text="{Binding EventTime}"
                                       HorizontalOptions="Center"
                                       FontSize="Default"/>
                    
                    <Label Grid.Column="3"
                                   Text="{Binding AwayEventMessage}"
                                   TextColor="White"
                                   FontSize="Medium" >
                        <Label.Behaviors>
                            <!--DOESN'T WORK-->
                            <toolkit:TouchBehavior LongPressCommand="{Binding LongPressCommand}"/>
                        </Label.Behaviors>
                    </Label>
                    
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
maui maui-community-toolkit community-toolkit-mvvm
1个回答
0
投票

将绑定用于行为时,您可以使用

x:Reference
绑定表达式。

假设您在 ViewModel 中有一个 ContentPage 绑定到的

LongPressCommand
。然后你可以使用
x:reference
元素的绑定表达式,如下所示,

首先将页面名称设置为“this”,

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
     ...
     x:Name="this">

使用

x:Reference
进行绑定,

<DataTemplate>
    ...
                <Grid.Behaviors>
                    <!--DOESN'T WORK-->
                    <toolkit:TouchBehavior LongPressCommand="{Binding BindingContext.LongPressCommand,source={x:Reference this}}"/>
                </Grid.Behaviors>
</DataTemplate>

如果您有任何疑问,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.