Advertisement
smeacham

Untitled

May 20th, 2016
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using Xamarin.Forms;
  8.  
  9. namespace ListViewDemoVS
  10. {
  11.     class MyContentPage : ContentPage
  12.     {
  13.         public MyContentPage()
  14.         {
  15.             Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
  16.  
  17.             ListView listView = new ListView();
  18.             listView.ItemsSource = new ListItem[] {
  19.                 new ListItem { Title = "First", Description = "1st" },
  20.                 new ListItem { Title = "Second", Description = "2nd" },
  21.                 new ListItem { Title = "Third", Description = "3rd" }
  22.             };
  23.  
  24.             listView.ItemTemplate = new DataTemplate(typeof(TextCell));
  25.             listView.ItemTemplate.SetBinding(TextCell.TextProperty, "Title");
  26.             listView.ItemTemplate.SetBinding(TextCell.DetailProperty, "Description");
  27. /*
  28.             listView.ItemTapped += async (sender, e) =>
  29.             {
  30.                 await DisplayAlert("Tapped", e.Item.ToString(), "OK");
  31.                 ((ListView)sender).SelectedItem = null;
  32.             };
  33. */
  34.             listView.ItemSelected += async (sender, e) =>
  35.             {
  36.                 if (e.SelectedItem == null) return;
  37.                 ListItem item = (ListItem)e.SelectedItem;
  38.                 await DisplayAlert("Selected", e.SelectedItem.ToString(), "OK");
  39.                 ((ListView)sender).SelectedItem = null;
  40.             };
  41.  
  42.             this.Content = listView;
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement