Advertisement
smeacham

ListItemCell.cs

May 24th, 2016
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 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 ListItemCell : ViewCell
  12.     {
  13.         public ListItemCell()
  14.         {
  15.             Label titleLabel = new Label
  16.             {
  17.                 HorizontalOptions = LayoutOptions.FillAndExpand,
  18.                 FontSize = 25,
  19.                 FontAttributes = Xamarin.Forms.FontAttributes.Bold,
  20.                 TextColor = Color.White
  21.             };
  22.             titleLabel.SetBinding(Label.TextProperty, "Title");
  23.  
  24.             Label descLabel = new Label
  25.             {
  26.                 HorizontalOptions = LayoutOptions.FillAndExpand,
  27.                 FontSize = 12,
  28.                 TextColor = Color.White
  29.             };
  30.             descLabel.SetBinding(Label.TextProperty, "Description");
  31.  
  32.             Label priceLabel = new Label
  33.             {
  34.                 HorizontalOptions = LayoutOptions.End,
  35.                 FontSize = 25,
  36.                 TextColor = Color.Aqua
  37.             };
  38.             priceLabel.SetBinding(Label.TextProperty, "Price");
  39.  
  40.             Button button = new Button
  41.             {
  42.                 Text = "Buy Now",
  43.                 BackgroundColor = Color.Teal,
  44.                 HorizontalOptions = LayoutOptions.EndAndExpand
  45.             };
  46.             button.SetBinding(Button.CommandParameterProperty, new Binding("."));
  47.  
  48.             button.Clicked += (sender, e) =>
  49.             {
  50.                 var b = (Button)sender;
  51.                 var item = (ListItem)b.CommandParameter;
  52.                 // Climb back up the layout tree from the button up
  53.                 // through the nested layouts and through the ListView
  54.                 // to retrieve the ContentPage.
  55.                 // WARNING: This line no longer works.  It causes an
  56.                 // invalid cast error at runtime.  It needs to be
  57.                 // investigated.
  58.                 ((ContentPage)((ListView)((StackLayout)((StackLayout)b.ParentView).ParentView).ParentView).ParentView).DisplayAlert("Clicked", item.Title.ToString() + " button was clicked", "OK");
  59.             };
  60.  
  61.             StackLayout viewLayoutItem = new StackLayout()
  62.             {
  63.                 HorizontalOptions = LayoutOptions.StartAndExpand,
  64.                 Orientation = StackOrientation.Vertical,
  65.                 Children = { titleLabel, descLabel }
  66.             };
  67.  
  68.             StackLayout viewLayout = new StackLayout()
  69.             {
  70.                 HorizontalOptions = LayoutOptions.StartAndExpand,
  71.                 Orientation = StackOrientation.Horizontal,
  72.                 Padding = new Thickness(25, 10, 55, 15),
  73.                 Children = { viewLayoutItem, priceLabel, button }
  74.             };
  75.  
  76.             View = viewLayout;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement