Поймать событие щелчка группы ExpandableListView в формах Xamarin - PullRequest
2 голосов
/ 10 мая 2019

Я хочу получить выбранный элемент для GroupHeaderTemplate в ExpandableListView.Но как поймать событие щелчка группы ExpandableListView в формах Xamarin, я нахожу решения для Android Xamarin, но не для форм.Кроме того, выбор элементов списка работает только для дочерних элементов.Будет полезна любая ссылка или ссылка.

Мой обновленный 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 Class

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

    }

1 Ответ

0 голосов
/ 10 мая 2019

Это просто для вашего первого ребенка 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);

И ваш приемник команд будет выглядеть примерно так:

 private void DoSomething(object obj)
    {

    }

И в этом obj объекте вы получите информацию о выбранном вами элементе типа, который связан с ListView. Обратите внимание, что это будет один object, а не collection

Обновление:

Свойство команды будет выглядеть примерно так:

public ICommand DoSomethingCommand{ get; set; }
...