我有一个包含项目的 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>
当使用绑定来表示行为时,您可以使用
x:Reference
绑定表达式。假设您有一个
LongPressCommand
ViewModel,ContentPage 绑定到该 ViewModel。然后,您可以x:reference
对元素使用绑定表达式,如下所示,首先,将页面名称设置为“this”,
用于
x:Reference
绑定,DataTemplate 中的 Label Control 也是如此。
如果您有任何疑问,请告诉我。