|
1 | | -# accordion-command-binding-xamarin |
2 | | -How to bind command in Xamarin.Forms Accordion (SfAccordion) |
| 1 | +# How to bind command in Xamarin.Forms Accordion (SfAccordion) |
| 2 | + |
| 3 | +You can bind the ViewModel [commands](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/commanding) to content of [AccordionItem](https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.Expander.XForms~Syncfusion.XForms.Accordion.AccordionItem.html?) in Xamarin.Forms [SfAccordion](https://help.syncfusion.com/xamarin/accordion/getting-started?). |
| 4 | + |
| 5 | +You can also refer the following article. |
| 6 | + |
| 7 | +https://www.syncfusion.com/kb/11359/how-to-bind-command-in-xamarin-forms-accordion-sfaccordion |
| 8 | + |
| 9 | +**C#** |
| 10 | + |
| 11 | +ViewModel command to show the Accordion details. |
| 12 | + |
| 13 | +``` c# |
| 14 | +namespace AccordionXamarin |
| 15 | +{ |
| 16 | + public class ItemInfoRepository |
| 17 | + { |
| 18 | + public Command<object> ShowDetailsCommand { get; set; } |
| 19 | + public ItemInfoRepository() |
| 20 | + { |
| 21 | + ShowDetailsCommand = new Command<object>(OnShowDetailClicked); |
| 22 | + } |
| 23 | + |
| 24 | + private void OnShowDetailClicked(object obj) |
| 25 | + { |
| 26 | + var item = obj as ItemInfo; |
| 27 | + App.Current.MainPage.DisplayAlert(item.Name, item.Description, "Ok"); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +**XAML** |
| 34 | + |
| 35 | +Binding the ViewModel command to the [ImageButton](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/imagebutton) that is loaded inside the [content](https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.Expander.XForms~Syncfusion.XForms.Accordion.AccordionItem~Content.html?) of AccordionItem. |
| 36 | + |
| 37 | +``` xml |
| 38 | +<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" |
| 39 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 40 | + xmlns:local="clr-namespace:AccordionXamarin" |
| 41 | + xmlns:accordion="clr-namespace:Syncfusion.XForms.Accordion;assembly=Syncfusion.Expander.XForms" |
| 42 | + x:Class="AccordionXamarin.MainPage"> |
| 43 | + |
| 44 | + <ContentPage.BindingContext> |
| 45 | + <local:ItemInfoRepository x:Name="viewModel"/> |
| 46 | + </ContentPage.BindingContext> |
| 47 | + |
| 48 | + <accordion:SfAccordion x:Name="accordion" BindableLayout.ItemsSource="{Binding Info}" ExpandMode="SingleOrNone"> |
| 49 | + <BindableLayout.ItemTemplate> |
| 50 | + <DataTemplate> |
| 51 | + <accordion:AccordionItem> |
| 52 | + <accordion:AccordionItem.Header> |
| 53 | + <Grid Padding="5,0,0,0" HeightRequest="50"> |
| 54 | + <Label Text="{Binding Name}" FontSize="20" /> |
| 55 | + </Grid> |
| 56 | + </accordion:AccordionItem.Header> |
| 57 | + <accordion:AccordionItem.Content> |
| 58 | + <Grid BackgroundColor="LightGray" Padding="5,0,0,0"> |
| 59 | + <Grid.ColumnDefinitions> |
| 60 | + <ColumnDefinition Width="*" /> |
| 61 | + <ColumnDefinition Width="45" /> |
| 62 | + </Grid.ColumnDefinitions> |
| 63 | + <Label Text="{Binding Description}" VerticalOptions="Center"/> |
| 64 | + <Grid Grid.Column="1" Padding="10" BackgroundColor="LightGray"> |
| 65 | + <ImageButton HeightRequest="30" WidthRequest="30" Source="Details.png" Command="{Binding Path=BindingContext.ShowDetailsCommand, Source={x:Reference accordion}}" CommandParameter="{Binding .}"/> |
| 66 | + </Grid> |
| 67 | + </Grid> |
| 68 | + </accordion:AccordionItem.Content> |
| 69 | + </accordion:AccordionItem> |
| 70 | + </DataTemplate> |
| 71 | + </BindableLayout.ItemTemplate> |
| 72 | + </accordion:SfAccordion> |
| 73 | +</ContentPage> |
| 74 | +``` |
0 commit comments