title: Binding a Custom View In Xamarin.Forms
body: <p>I have a problem binding data in a custom view in Xamarin forms to the view model of the containing page.</p>
  
  <p>My Custom View is very simple, a pair of labels representing a key value pair:</p>
  
  <pre><code>&lt;ContentView xmlns="http://xamarin.com/schemas/2014/forms"
               xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
               x:Class="KeyValueView"&gt;
      &lt;Grid VerticalOptions="Start"&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition /&gt;
              &lt;ColumnDefinition /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
  
          &lt;Label x:Name="KeyLabel" Text="{Binding Key}"  Grid.Column="0" HorizontalOptions="Start" /&gt;
          &lt;Label x:Name="ValueLabel" Text="{Binding Value}" Grid.Column="1" HorizontalOptions="EndAndExpand" /&gt;
      &lt;/Grid&gt;
  &lt;/ContentView&gt;
  </code></pre>
  
  <p>with the code behind:</p>
  
  <pre><code>public partial class KeyValueView : ContentView
  {
      public KeyValueView()
      {
          InitializeComponent();
          this.VerticalOptions = LayoutOptions.Start;
          this.BindingContext = this;
      }
  
      public static readonly BindableProperty ValueProperty =
                  BindableProperty.Create&lt;KeyValueView, string&gt;(w =&gt; w.Value, default(string));
  
      public string Value
      {
          get {return (string)GetValue(ValueProperty);}
          set {SetValue(ValueProperty, value);}
      }
  
      public static readonly BindableProperty KeyProperty =
          BindableProperty.Create&lt;KeyValueView, string&gt;(w =&gt; w.Key, default(string));
  
      public string Key
      {
          get {return (string)GetValue(KeyProperty);}
          set {SetValue(KeyProperty, value);}
      }
  }
  </code></pre>
  
  <p>This is consumed in a page as follows:</p>
  
  <pre><code>&lt;views:KeyValueView Key="Order Number" Value="{Binding document_number}" /&gt;
  </code></pre>
  
  <p>The problem is that the Key string is displayed as expected, but the value string is not.
  I have tried forcing a PropertyChanged event on the document_number property, this did not help.
  I have also tried explicitly setting the Text property on the Labels during the setter of the Key/Value properties of the custom view:</p>
  
  <pre><code>public string Key
      {
          get {return (string)GetValue(KeyProperty);}
          set {
              SetValue(KeyProperty, value);
              KeyLabel.Text = value;
          }
      }
  </code></pre>
  
  <p>Again this did not help, the setter code never seems to get executed (I placed a breakpoint on it and it was not hit)</p>
  
  <p>If I add an out of the box control such as a Label bound directly to the property in the page, this displays correctly:</p>
  
  <pre><code>&lt;Label Text="{Binding document_number}"/&gt;
  &lt;views:KeyValueView Key="Order Number" Value="{Binding document_number}" /&gt;
  </code></pre>
  
  <p>Can anyone explain why this is happening (or rather not happening)?</p>