SfListView ItemTapped avoiding item selection - Error on ItemTapped following tasks

Hello all,

I'm trying to start a event after Item Selection. This event takes some value of the item selected and atributes o a Settings Var.

But if I insert the ItemTapped on the SfListView class on XAML, the item selection does not occur and App goes exception.

Please see the code bellow:

XAML page:

<syncfusion:SfListView x:Name="ListView1" ItemsSource="{Binding NearProductsList}" SelectionMode="Single" ItemTapped="Item_Tapped" Orientation="Horizontal" ItemSpacing="0,0,0,0" AutoFitMode="None" ItemSize="200" HeightRequest="150" IsScrollBarVisible="False">

<syncfusion:SfListView.ItemTemplate>

<DataTemplate>

<Grid >

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto"/>

<ColumnDefinition/>

</Grid.ColumnDefinitions>

<Grid.RowDefinitions>

<RowDefinition Height="*"/>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

</Grid.RowDefinitions>

<Image Source = "NoImage.png" HorizontalOptions = "Center" VerticalOptions = "Start" />

<Label Grid.Row="1" LineBreakMode="NoWrap" Text="{Binding ProductName}" TextColor="#292a60" FontSize="10" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>

<Label Grid.Row="2" LineBreakMode="NoWrap" Text="{Binding ProductPriceBoleto, StringFormat='A partir de R${0}'}" TextColor="#292a60" FontSize="10" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>

</Grid>

</DataTemplate>

</syncfusion:SfListView.ItemTemplate>

</syncfusion:SfListView>



The code Behind:


public Home()




{


InitializeComponent();



NavigationPage.SetHasNavigationBar(this, false);


Padding = new Thickness(0, 10, 0, 0);




Color Amarelo = Color.FromRgb(212, 217, 75);


Color Azul = Color.FromRgb(41, 42, 96);


BackgroundColor = Azul;


}


 



private async void Item_Tapped(object sender, ItemTappedEventArgs e)




{



var SearchNear = ListView1.SelectedItem as NearProducts; //As ItemTapped avoid item selection, The ListView1.SelectItem always comes null. NearProducts come from model class.




Settings.SelectedProduct = SearchNear.BusinessName; //Error occurs here as Settings.SelectedProduct are string.



await Navigation.PushAsync(new ProductListPage(), false);




}


Thanks in advance.


Daniel.


1 Reply

MK Muthu Kumaran Gnanavinayagam Syncfusion Team April 30, 2018 07:30 AM UTC

Hi Daniel, 
 
We have checked the reported query “Need to set Current SelectedItem of SfListView to a property using ItemTapped event” from our side. We would like to let you know that the ItemTapped event will be fired once touch is passed to the SfListView item. At that time, the Selection will not be processed and hence the SelectedItem is got as ‘null’. So you can use the SelectionChanged event as like below to achieve your requirement. 
 
Code Example[C#]: 
    public partial class MainPage : ContentPage   
    { 
        public MainPage() 
        { 
            InitializeComponent(); 
            listView.SelectionChanged += ListView_SelectionChanged; 
        } 
 
        private void ListView_SelectionChanged(object sender, ItemSelectionChangedEventArgs e) 
        { 
            var newPage = new NewPage(); 
            newPage.BindingContext = (sender as SfListView).SelectedItems; 
            Navigation.PushAsync(newPage); 
        } 
    } 
 
For your reference, we have attached the sample and you can download it from the below link. 
 
 
Please let us know if you require further assistance. 
 
Regards, 
G.Muthu kumaran. 


Loader.
Up arrow icon