繁体   English   中英

Xamarin Forms CollectionView 点击手势或选择更改不起作用

[英]Xamarin Forms CollectionView Tap Gesture or Selection Changed not working

Xamarin.Forms 我的问题至少存在于 android 上。

我有一个使用分组的 collectionview。 我想让每个 collectionview 组都可以点击。 我曾尝试在第二个数据模板中的 StackLayout、顶级数据模板中的标签和 CollectionView 本身中添加一个 tapGesture 识别器,我也尝试过它们作为 tapped 和 command。 最后,我在 collectionView 上尝试了 SelectionChanged。

单击 collectionview 上的任意位置不会在 ViewModel 或 View.cs 中出现断点

我想要它,所以点击一个项目将为 ViewModel 提供一个排序参数,以便它知道 CollectionView 中的哪个项目被点击

这是我的 XAML,如果有人能看到我犯了什么错误,那就太棒了! 谢谢

<CollectionView Grid.Row="1"  ItemsSource="{Binding ResponseCollection}" SelectionMode="Single" SelectionChanged="ResponseCollectionView_SelectionChanged" x:Name="ResponseCollectionView" IsGrouped="True" >

                <CollectionView.GroupHeaderTemplate>
                    <DataTemplate >
                        <Label BackgroundColor="White" Padding="10,10,10,0" Text="{Binding Response}">
                        </Label>

                    </DataTemplate>
                </CollectionView.GroupHeaderTemplate>

                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <ScrollView>
                            <Grid Padding="10" BackgroundColor="White">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="1"/>
                                </Grid.RowDefinitions>

                                <StackLayout Grid.Row="0" Orientation="Horizontal">

                                    <Label TextColor="Red" Text="{Binding Statistic}"/>
                                    <Label TextColor="Red" Text="{Binding Amount}"/>
                                </StackLayout>
                                
                                <BoxView Grid.Row="1" BackgroundColor="Gray" HorizontalOptions="FillAndExpand"></BoxView>
                            </Grid>
                        </ScrollView>
                    </DataTemplate>
                </CollectionView.ItemTemplate>

            </CollectionView>

使用相对绑定

  1. 为容器(或 ContentPage)添加名称
  2. 为您的点击命令绑定使用相对绑定
<StackLayout x:Name="PageView">
    <CollectionView>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding Path=BindingContext.ViewModelCommand, Source={x:Reference PageView}}" />
                    </StackLayout.GestureRecognizers>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</StackLayout>

更新以包含参数:

<TapGestureRecognizer
    Command="{Binding Path=BindingContext.ViewModelCommand, Source={x:Reference PageView}}"
    CommandParameter="{Binding .}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM