|
1 | | -# event-to-command-prism-listview-xamarin |
2 | | -How to work with Prism using the EventToCommandBehavior in Xamarin.Forms ListView (SfListView) |
| 1 | +# How to work with Prism using the EventToCommandBehavior in Xamarin.Forms ListView (SfListView) |
| 2 | + |
| 3 | +You can pass the default event argument as parameter when using [Prism](https://prismlibrary.com/docs/index.html) Framework EventToCommandBehavior in Xamarin.Forms [SfListView](https://help.syncfusion.com/xamarin/listview/overview). |
| 4 | + |
| 5 | +The prism [EventToCommandBehavior](https://prismlibrary.com/docs/xamarin-forms/behaviors/eventtocommandbehavior.html) will pass the parameter based on the [CommandParameter](https://prismlibrary.com/docs/xamarin-forms/behaviors/eventtocommandbehavior.html#commandparameter), [EventArgsParameterPath](https://prismlibrary.com/docs/xamarin-forms/behaviors/eventtocommandbehavior.html#eventargsparameterpath) and [EventArgsConverter](https://prismlibrary.com/docs/xamarin-forms/behaviors/eventtocommandbehavior.html#eventargsconverter). The parameter will be null in the [Command](https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/commanding) execute method, if no value passed to these parameters. |
| 6 | + |
| 7 | +You can also refer the following article |
| 8 | +https://www.syncfusion.com/kb/11680/how-to-work-with-prism-using-the-eventtocommandbehavior-in-xamarin-forms-listview |
| 9 | + |
| 10 | +**XAML** |
| 11 | + |
| 12 | +Refer the Prism Framework **EventToCommandBehavior** for ListView. Use **EventArgsConverter** to pass the default EventArgs to the **Command**. |
| 13 | +``` xml |
| 14 | +<?xml version="1.0" encoding="utf-8" ?> |
| 15 | +<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" |
| 16 | + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
| 17 | + xmlns:local="clr-namespace:ListViewXamarin" |
| 18 | + xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms" |
| 19 | + xmlns:helper="clr-namespace:ListViewXamarin.Helper" |
| 20 | + xmlns:behavior="http://prismlibrary.com" |
| 21 | + x:Class="ListViewXamarin.MainPage"> |
| 22 | + |
| 23 | + <ContentPage.Resources> |
| 24 | + <ResourceDictionary> |
| 25 | + <helper:Converter x:Key="EventArgsConverter"/> |
| 26 | + </ResourceDictionary> |
| 27 | + </ContentPage.Resources> |
| 28 | + <ContentPage.Content> |
| 29 | + <StackLayout> |
| 30 | + <syncfusion:SfListView x:Name="listView" ItemSize="60" ItemsSource="{Binding ContactsInfo}"> |
| 31 | + <syncfusion:SfListView.Behaviors> |
| 32 | + <behavior:EventToCommandBehavior EventName="ItemDoubleTapped" Command="{Binding ItemDoubleTappedCommand}" EventArgsConverter="{StaticResource EventArgsConverter}"/> |
| 33 | + </syncfusion:SfListView.Behaviors> |
| 34 | + <syncfusion:SfListView.ItemTemplate> |
| 35 | + <DataTemplate> |
| 36 | + <Grid x:Name="grid"> |
| 37 | + <Grid.ColumnDefinitions> |
| 38 | + <ColumnDefinition Width="70" /> |
| 39 | + <ColumnDefinition Width="*" /> |
| 40 | + </Grid.ColumnDefinitions> |
| 41 | + <Image Source="{Binding ContactImage}" VerticalOptions="Center" HorizontalOptions="Center" HeightRequest="50" WidthRequest="50"/> |
| 42 | + <Grid Grid.Column="1" RowSpacing="1" Padding="10,0,0,0" VerticalOptions="Center"> |
| 43 | + <Label x:Name="label" LineBreakMode="NoWrap" TextColor="#474747" Text="{Binding ContactName}"/> |
| 44 | + <Label Grid.Row="1" Grid.Column="0" TextColor="#474747" LineBreakMode="NoWrap" Text="{Binding ContactNumber}"/> |
| 45 | + </Grid> |
| 46 | + </Grid> |
| 47 | + </DataTemplate> |
| 48 | + </syncfusion:SfListView.ItemTemplate> |
| 49 | + </syncfusion:SfListView> |
| 50 | + </StackLayout> |
| 51 | + </ContentPage.Content> |
| 52 | +</ContentPage> |
| 53 | +``` |
| 54 | +**C#** |
| 55 | + |
| 56 | +Return the value which is the actual parameter of the Event. |
| 57 | +``` c# |
| 58 | +namespace ListViewXamarin.Helper |
| 59 | +{ |
| 60 | + public class Converter : IValueConverter |
| 61 | + { |
| 62 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 63 | + { |
| 64 | + return value; |
| 65 | + } |
| 66 | + |
| 67 | + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
| 68 | + { |
| 69 | + throw new NotImplementedException(); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
0 commit comments