在Xamarin表单中捕获ExpandableListView组click事件

时间:2019-05-10 08:09:03

标签: xamarin xamarin.forms expandablelistview

我希望在ExpandableListView中为 GroupHeaderTemplate 选择项目。但是,如何在Xamarin Forms中捕获ExpandableListView组单击事件,我发现了针对Xamarin Android的解决方案,但没有针对Forms的解决方案。此外,列表项选择仅适用于子项,如何获得组项的单击事件。任何链接或参考都将有所帮助。

我更新的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>

ViewModel 在构造函数中:

 ShowDetailCommand = new Command<StaffLoanPageViewModel>(Showdetail);

ViewModel类

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

    }

1 个答案:

答案 0 :(得分:0)

在您的第一个孩子ViewCell中很容易,只需添加一个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);

您的Commands接收器将如下所示:

 private void DoSomething(object obj)
    {

    }

在此obj对象中,您将获得与ListView绑定的类型的单击项的详细信息,请注意,这将是单个object而不是{{1 }}

更新:

command属性如下所示:

collection