Xamarin : OnTapped (on clicked) item in listview




Xamarin : OnTapped (on clicked) item in listview


  • Add listview  in page

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
 x:Class="App1.MainMaster"
 Title="My Application">
  <ListView x:Name="MainListItemTapped="OnTapped">
<ListView.ItemTemplate>
  <DataTemplate>
<ViewCell>
  <StackLayout Orientation="Horizontal">
<Label Text="{Binding Name}"></Label>
<Label Text="{Binding Description}"></Label>
<Label Text="{Binding OrderNumber}"></Label>
  </StackLayout>
</ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>
  </ListView>
</ContentPage>


  • Write a code to bind listview 

public partial class MainMaster : ContentPage
{
public MainMaster()
{
InitializeComponent();
MainList.ItemsSource = new List<ListViewTemplate>
{
new ListViewTemplate { Name="One", Description="One", OrderNumber=1 },
new ListViewTemplate { Name="Two", Description="Two", OrderNumber=2 },
new ListViewTemplate { Name="Three", Description="Three", OrderNumber=3 }, new ListViewTemplate { Name="Four", Description="Four", OrderNumber=4 },
new ListViewTemplate { Name="Five", Description="Five", OrderNumber=5 },
};
}
}

        void OnTapped(object sender, ItemTappedEventArgs e)
        {
            var selected = e.Item as ListViewTemplate;
            DisplayAlert("Item Selecte", selected.Name, "Okay");
        }


  • Set page in to run the application

public class App : Application
{
public App()
{
MainPage = new MainMaster();
}

}

  • Run the application




No comments:

Post a Comment