Advertisement
smeacham

MyContentPage.cs

May 24th, 2016
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 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", Source="one.jpg" },
  20.                 new ListItem { Title = "Second", Description = "2nd", Source="two.jpg" },
  21.                 new ListItem { Title = "Third", Description = "3rd", Source="three.jpg" }
  22.             };
  23.  
  24.             listView.ItemTemplate = new DataTemplate(typeof(ImageCell));
  25.             listView.ItemTemplate.SetBinding(ImageCell.TextProperty, "Title");
  26.             listView.ItemTemplate.SetBinding(ImageCell.DetailProperty, "Description");
  27.             listView.ItemTemplate.SetBinding(ImageCell.ImageSourceProperty, "Source");
  28.             /*
  29.                         listView.ItemTapped += async (sender, e) =>
  30.                         {
  31.                             await DisplayAlert("Tapped", e.Item.ToString(), "OK");
  32.                             ((ListView)sender).SelectedItem = null;
  33.                         };
  34.             */
  35.             listView.ItemSelected += async (sender, e) =>
  36.             {
  37.                 if (e.SelectedItem == null) return;
  38.                 ListItem item = (ListItem)e.SelectedItem;
  39.                 await DisplayAlert("Selected", ((ListItem)(e.SelectedItem)).Title, "OK");
  40.                 ((ListView)sender).SelectedItem = null;
  41.             };
  42.  
  43.             this.Content = listView;
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement