在 Xamarin Forms 中捕获 ExpandableListView 组单击事件

Catch ExpandableListView group click event in Xamarin Forms

我希望在 ExpandableListView 中为 GroupHeaderTemplate 获取所选项目。但是如何在 Xamarin Forms 中捕获 ExpandableListView 组单击事件,我正在为 Xamarin Android 找到解决方案,但为 Forms 找到 none 。此外,列表项选择仅适用于子项,如何获取组项的单击事件。任何 Link 或参考资料都会有所帮助。

我的更新Xaml

  <ListView VerticalOptions="FillAndExpand"
                                x:Name="HotelsList"  SeparatorColor="Black"
                                BackgroundColor="Transparent" ItemSelected="HotelsList_ItemSelected"
                                IsGroupingEnabled="True" 
                                IsPullToRefreshEnabled="true"
                                ItemsSource="{Binding StaffLoansList}"
                                RefreshCommand="{Binding LoadHotelsCommand}" >

                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>

                                    <Grid VerticalOptions="StartAndExpand" Margin="10" >
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="30"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="100"/>

                                        </Grid.ColumnDefinitions>
                                        <Image  Grid.Column="0" Aspect="AspectFit"  HeightRequest="20" WidthRequest="30" 
                                                       HorizontalOptions="StartAndExpand" Margin="5"
                                                       VerticalOptions="CenterAndExpand">
                                            <Image.Source>
                                                <FileImageSource File="{Binding ImageStatus}" />
                                            </Image.Source>
                                        </Image>
                                        <Label Grid.Column="1" Text="{Binding actionName}" FontSize="15" HorizontalTextAlignment="Start"
                                                       HorizontalOptions="Start"
                                                       VerticalOptions="CenterAndExpand" />
                                        <Label Grid.Column="2" Text="{Binding actionDate}" FontSize="15"
                                                       HorizontalOptions="EndAndExpand"
                                                       VerticalOptions="CenterAndExpand" />
                                    </Grid>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                        <ListView.GroupHeaderTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <Grid>
                                        <Label
                                            FontSize="16"
                                            Text="{Binding Name}"
                                            TextColor="Gray" 
                                            VerticalOptions="Center">


                                        </Label>

                                        <Image x:Name="ImgA" Source="{Binding StateIcon}"  Margin="0,0,5,0" HeightRequest="20" WidthRequest="20" HorizontalOptions="End">

                                            <Image.GestureRecognizers>
                                                <TapGestureRecognizer Command="{Binding Path=BindingContext.DummyCommand, Source={x:Reference HotelsList}}" NumberOfTapsRequired="1" CommandParameter="{Binding .}"/>

                                            </Image.GestureRecognizers>
                                        </Image>
                                        <Grid.GestureRecognizers>
                                            <TapGestureRecognizer Command="{Binding Path=BindingContext.ShowDetailCommand, Source={x:Reference HotelsList}}" CommandParameter="{Binding .}"/>

                                        </Grid.GestureRecognizers>
                                    </Grid>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.GroupHeaderTemplate>
                    </ListView>

视图模型 在构造函数中:

 ShowDetailCommand = new Command<StaffLoanPageViewModel>(Showdetail);

视图模型Class

 public ICommand ShowDetailCommand { get; set; }
 public void Showdetail(object obj)
    {
        NavigationService.NavigateTo(ViewModelLocator.StaffLoanDetailPopupPage, Staffloans);

    }

ViewCell 的第一个 child 中这很简单,只需添加一个 GestureRecognizor

<ViewCell>
 <Grid>
 .
 . 
 .
 <Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.DoSomethingCommand, Source={x:Reference HotelsList}}"
                   CommandParameter="{Binding .}"/>
 </Grid.GestureRecognizers>
 </Grid>
 </ViewCell>

然后在你的命令中接收这个点击事件

DoSomethingCommand= new Command(DoSomething);

你的命令接收器看起来像这样:

 private void DoSomething(object obj)
    {

    }

并且在此 obj object 中,您将获得与 ListView 绑定的类型的单击项目的详细信息,请注意这将是单个 object而不是 collection

更新:

命令 属性 看起来像这样:

public ICommand DoSomethingCommand{ get; set; }