|
1 | | -# tap-command-button-click-handling-listview-xamarin |
2 | | -How to handle click action with tap command in Xamarin.Forms ListView (SfListView) |
| 1 | +# How to handle click action with tap command in Xamarin.Forms ListView (SfListView) |
| 2 | + |
| 3 | +You can add button inside [ListViewItem](https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.ListViewItem.html) and handle both ItemTapped and Button click action in Xamarin.Forms [SfListView](https://help.syncfusion.com/xamarin/listview/overview). |
| 4 | + |
| 5 | +**XAML** |
| 6 | + |
| 7 | +Load Button control inside the [SfListView.ItemTemplate](https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.SfListView~ItemTemplate.html) and bind [SfListView.TapCommand](https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.SfListView~TapCommand.html) and [Button.Command](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/button#using-the-command-interface). |
| 8 | + |
| 9 | +``` xml |
| 10 | +<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" |
| 11 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 12 | + xmlns:local="clr-namespace:ListViewXamarin" |
| 13 | + xmlns:sflistview="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms" |
| 14 | + x:Class="ListViewXamarin.MainPage"> |
| 15 | + <ContentPage.BindingContext> |
| 16 | + <local:AccordionViewModel/> |
| 17 | + </ContentPage.BindingContext> |
| 18 | + <ContentPage.Content> |
| 19 | + <Grid x:Name="mainGrid" BackgroundColor="#F0F0F0" Padding="4"> |
| 20 | + <sflistview:SfListView x:Name="listView" AutoFitMode="DynamicHeight" ItemsSource="{Binding ContactsInfo}" SelectionMode ="None" TapCommand="{Binding ItemTappedCommand}" IsScrollBarVisible="False"> |
| 21 | + <sflistview:SfListView.ItemTemplate> |
| 22 | + <DataTemplate> |
| 23 | + <Grid Padding="2" Margin="1" x:Name="viewCell" BackgroundColor="#F0F0F0" > |
| 24 | + <Frame x:Name="frame" CornerRadius="2" Padding="1" Margin="1" OutlineColor="White"> |
| 25 | + ... |
| 26 | + <Label Grid.Row="0" LineBreakMode="NoWrap" TextColor="#474747" Text="{Binding ContactName}" FontSize="16"/> |
| 27 | + <Label Grid.Row="1" TextColor="#474747" LineBreakMode="NoWrap" Text="{Binding CallTime}" FontSize="12"/> |
| 28 | + <Button Grid.Row="2" Text="Details" x:Name="button" Command="{Binding Source={x:Reference listView}, Path=BindingContext.ButtonCommand}" CommandParameter="{Binding .}"/> |
| 29 | + </Grid> |
| 30 | + <Grid Grid.Row="0" Grid.Column="2" HorizontalOptions="Center" VerticalOptions="Center"> |
| 31 | + <Image Source="{Binding PhoneImage}" Opacity="0.60" HeightRequest="20" WidthRequest="20" HorizontalOptions="Center" VerticalOptions="Center" /> |
| 32 | + </Grid> |
| 33 | + </Grid> |
| 34 | + </Grid> |
| 35 | + <Grid IsVisible="{Binding IsVisible, Mode=TwoWay}" ColumnSpacing="0" RowSpacing="0" Grid.Row="1" BackgroundColor="White" |
| 36 | + HorizontalOptions="FillAndExpand" Padding="5" VerticalOptions="FillAndExpand"> |
| 37 | + ... |
| 38 | + </Grid> |
| 39 | + </Grid> |
| 40 | + </Frame> |
| 41 | + </Grid> |
| 42 | + </DataTemplate> |
| 43 | + </sflistview:SfListView.ItemTemplate> |
| 44 | + </sflistview:SfListView> |
| 45 | + </Grid> |
| 46 | + </ContentPage.Content> |
| 47 | +</ContentPage> |
| 48 | +``` |
| 49 | + |
| 50 | +**C#** |
| 51 | + |
| 52 | +Expand the Accordion view in the **TapCommand** method and display the contact details in the **ButtonCommand** method. |
| 53 | +``` c# |
| 54 | +public class AccordionViewModel |
| 55 | +{ |
| 56 | + public ObservableCollection<Contact> ContactsInfo { get; set; } |
| 57 | + internal Contact TappedItem { get; set; } |
| 58 | + public Command<object> ButtonCommand { get; set; } |
| 59 | + public Command<object> ItemTappedCommand { get; set; } |
| 60 | + |
| 61 | + public AccordionViewModel() |
| 62 | + { |
| 63 | + ButtonCommand = new Command<object>(OnButtonTapped); |
| 64 | + ItemTappedCommand = new Command<object>(OnItemTapped); |
| 65 | + } |
| 66 | + |
| 67 | + private void OnItemTapped(object obj) |
| 68 | + { |
| 69 | + var ItemData = (obj as Syncfusion.ListView.XForms.ItemTappedEventArgs).ItemData as Contact; |
| 70 | + if (this.TappedItem == null) |
| 71 | + { |
| 72 | + //Expands when tap on the item at first. |
| 73 | + ItemData.IsVisible = true; |
| 74 | + this.TappedItem = ItemData; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + if (ItemData != this.TappedItem) |
| 79 | + { |
| 80 | + //Expands when tap on the another item. |
| 81 | + this.TappedItem.IsVisible = false; |
| 82 | + ItemData.IsVisible = true; |
| 83 | + this.TappedItem = ItemData; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + this.TappedItem.IsVisible = false; |
| 88 | + this.TappedItem = null; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void OnButtonTapped(object obj) |
| 94 | + { |
| 95 | + var item = obj as Contact; |
| 96 | + App.Current.MainPage.DisplayAlert(""+item.ContactName, "" + item.CallTime, "Ok"); |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +**Output** |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
0 commit comments