title: Xamarin Forms - access databound object in ViewCell
body: <p>I am creating a ListView whose items I want to directly control with code.  I have the following xaml that declares a generic ListView.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
               xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
               xmlns:local="clr-namespace:ViewCellClick"
               x:Class="ViewCellClick.MainPage"&gt;
      &lt;ListView x:Name="___listview" HasUnevenRows="True"&gt;
          &lt;ListView.ItemTemplate&gt;
          &lt;/ListView.ItemTemplate&gt;
      &lt;/ListView&gt;
  &lt;/ContentPage&gt;
  </code></pre>
  
  <p>and then in the code behind, I set up the ItemsSource property.</p>
  
  <pre><code>namespace ViewCellClick
  {
      public partial class MainPage : ContentPage
      {
          public MainPage()
          {
              InitializeComponent();
              ___listview.ItemsSource = Repository.GetList();
              ___listview.ItemTemplate = new DataTemplate(typeof(CustomViewCell));
          }
      }
  </code></pre>
  
  <p>and this leverages a custom template defined as...</p>
  
  <pre><code>    public class CustomViewCell : ViewCell
      {
          public CustomViewCell()
          {
              var stack = new StackLayout();
  
              stack.Children.Add(new Label() { Text = "Label" });
  
              // HOW DO I ACCESS THE MODEL HERE SO I CAN DRAW CUSTOM UI DEPENDING ON THE MODEL INSTANCE?
  
              View = stack;
          }
      }
  }
  </code></pre>
  
  <p>at this level, I have a very generic custom view cell as it only creates a label.  However, say the model is complicated, and depending on the data in the model I would draw a different UI for that ViewCell instance.  </p>
  
  <p>How do I get access to the model for each CustomViewCell and then draw the UI according to that model?</p>
  
  <p>I tried BindingContext, but it's null in the CustomViewCell constructor.</p>
  
  <p><a href="https://i.stack.imgur.com/hbCD4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hbCD4.png" alt="enter image description here"></a></p>