Advertisement
smeacham

MyContentPage.cs

Jun 2nd, 2016
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. using Xamarin.Forms;
  9.  
  10. namespace ListViewDemoVS
  11. {
  12.     class MyContentPage : ContentPage
  13.     {
  14.         ObservableCollection<Item> items = new ObservableCollection<Item>
  15.             {
  16.                 new Item { Title = "First", Description = "1st" },
  17.                 new Item { Title = "Second", Description = "2nd" },
  18.                 new Item { Title = "Third", Description = "3rd" }
  19.             };
  20.  
  21.         public MyContentPage()
  22.         {
  23.             Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
  24.  
  25.             var listView = new ListView();
  26.  
  27.             listView.ItemsSource = items;
  28.             listView.ItemTemplate = new DataTemplate(typeof(TextCell));
  29.             listView.ItemTemplate.SetBinding(TextCell.TextProperty, "Title");
  30.             listView.ItemTemplate.SetBinding(TextCell.DetailProperty, "Description");
  31.  
  32.             listView.ItemTapped += async (sender, e) =>
  33.             {
  34.                 Item item = (Item)e.Item;
  35.                 await DisplayAlert("Tapped", item.Title.ToString() + " was selected.", "OK");
  36.                 ((ListView)sender).SelectedItem = null;
  37.             };
  38.  
  39.             Button buttonRemove = new Button
  40.             {
  41.                 Text = "Delete Row",
  42.                 FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Button)),
  43.                 HorizontalOptions = LayoutOptions.Center,
  44.                 VerticalOptions = LayoutOptions.Fill
  45.             };
  46.  
  47.             buttonRemove.Clicked += (sender, e) =>
  48.             {
  49.                 items.RemoveAt(0);
  50.             };
  51.  
  52.             Content = new StackLayout
  53.             {
  54.                 Children = { buttonRemove, listView }
  55.             };
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement