- Home
- Forum
- Xamarin.Forms
- Sflistview -Set the Bindingcontext for command and command parameter in a listview item
Sflistview -Set the Bindingcontext for command and command parameter in a listview item
I am using sflistview to display list of items (uses samplelistviewmodel) and having a datatemplate for my viewcell (uses sampleitemviewmodel) .
ViewCell.View >
Inside the view cell i have a grid , with a Tap gesture recoognizer .This recognizer would call a command from a samplelistviewmodel and pass the sampleitemviewmodel as a Commandparameter to that command .
<xForms:SfListView
Grid.Row="2"
SelectionBackgroundColor="Transparent"
BackgroundColor="#F0F4F7"
ItemSize="270"
ItemsSource="{Binding ItemList}"
>
<xForms:SfListView.ItemTemplate>
<DataTemplate>
<views: SampleViewCell />
DataTemplate>
xForms:SfListView.ItemTemplate>
xForms:SfListView>
Grid.Row="2"
SelectionBackgroundColor="Transparent"
BackgroundColor="#F0F4F7"
ItemSize="270"
ItemsSource="{Binding ItemList}"
>
<xForms:SfListView.ItemTemplate>
<DataTemplate>
<views: SampleViewCell />
DataTemplate>
xForms:SfListView.ItemTemplate>
xForms:SfListView>
My code snippet is as follows,
<ViewCell
x:Class=""
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls=""
xmlns:xamlMarkupExtensions=""
x:Name="SampleViewCell"
>
x:Class=""
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls=""
xmlns:xamlMarkupExtensions=""
x:Name="SampleViewCell"
>
<ViewCell.View >
Command="{Binding Source={x:Reference SampleViewCell}, Path=Parent.BindingContext.SampleCommand}"
CommandParameter="{Binding Source={x:Reference SampleViewCell}, Path=BindingContext}"
However the same works , when i try with listview it works , while with sflitview it doesnt.Please help
SIGN IN To post a reply.
1 Reply
SP
Subburaj Pandian Veluchamy
Syncfusion Team
May 28, 2019 01:55 PM UTC
Hi Ramya,
Thank you for contacting Syncfusion support.
We would like to inform you that in SfListView, BindingContext of the ItemTemplate will be Model class for the Template items. So, when you define the Command for ViewCell, it is necessary to initialize the command in Model as following.
|
MessageInfoModel.cs
public class MessageInfo : INotifyPropertyChanged
{
public Command<object> SampleCommand { get; }
public MessageInfo()
{
SampleCommand = new Command<object>(OnGridTapped);
}
private void OnGridTapped(object obj)
{
var param = obj as MessageInfo;
App.Current.MainPage.DisplayAlert("Message","Tapped Item :"+param.Text,"Ok");
}
}
IncomingTextTemplate.xaml
<ViewCell >
<Grid ColumnSpacing="2" x:Name="Grid">
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Source={x:ReferenceGrid}, Path=BindingContext.SampleCommand}" CommandParameter="{Binding Source={x:Reference Grid}, Path=BindingContext}"/>
</Grid.GestureRecognizers>
</ViewCell >
|
We have attached the model sample for your reference,
Please let us know if you need any further assistance.
Regards,
Subburaj Pandian V
Subburaj Pandian V
SIGN IN To post a reply.
- 1 Reply
- 2 Participants
-
RA Ramya
- May 27, 2019 10:25 AM UTC
- May 28, 2019 01:55 PM UTC