title: Xamarin - Binding to a Property of a child control inside a custom control
body: <p>I am working on a Xamarin App.  I am making a custom control with a Label control, a DatePicker control and an Entry control.  I had to create quite a few BindableProperties for the Date Control in the Custom Control such as MaximumDate, MinimumDate Property among many other properties.  As far as I understand, the reason I have to create these BindableProperty members in my Custom control are because I have no access to the properties of the child controls when the custom control is used on a view.  Is there a way to access the properties of the child control that is embedded in a custom control? I could save a lot of lines of code defining the BindableProperties and their CLR properties and other things.</p>
  <p>Here is my Custom control XAML (I have removed all the elements except for a Label element in the posted code to make the code more readable and for brevity.</p>
  <pre><code>&lt;StackLayout xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot; 
           xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
           x:Class=&quot;DPSReminders.Controls.CustomLabel&quot;
           xmlns:controls=&quot;clr-namespace:DPSReminders.Controls&quot;
           xmlns:sfLayout=&quot;clr-namespace:Syncfusion.XForms.TextInputLayout;assembly=Syncfusion.Core.XForms&quot;
           xmlns:sfPicker=&quot;clr-namespace:Syncfusion.XForms.Pickers;assembly=Syncfusion.SfPicker.XForms&quot;
           xmlns:xct=&quot;http://xamarin.com/schemas/2020/toolkit&quot;
           xmlns:fai=&quot;clr-namespace:FontAwesome&quot;&gt;
  &lt;StackLayout.Resources&gt;
      &lt;ResourceDictionary&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/StackLayout.Resources&gt;
  &lt;StackLayout Orientation=&quot;Horizontal&quot; Margin=&quot;10&quot;&gt;
      &lt;Label x:Name=&quot;myLabel&quot; 
              Text=&quot;&quot;
              FontFamily=&quot;FASolid&quot;
              VerticalOptions=&quot;Center&quot;
              HorizontalOptions=&quot;Start&quot;
              Margin=&quot;10&quot;&gt;
      &lt;/Label&gt;
  &lt;/StackLayout&gt;
  </code></pre>
  
  <p>The code behind file:</p>
  <pre><code>public class CustomLabel : StackLayout
  {
  public static readonly BindableProperty LabelTextProperty =
  BindableProperty.Create(nameof(LabelText), typeof(string), typeof(CustomLabel),
      defaultBindingMode: BindingMode.TwoWay,
      propertyChanged: LabelTextPropertyChanged);
  
  public string LabelText
  {
      get =&gt; GetValue(LabelTextProperty)?.ToString();
      set =&gt; SetValue(LabelTextProperty, value);
  }
  
  private static void LabelTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
  {
      var control = bindable as CustomLabel;
      control.myLabel.Text = newValue?.ToString();
  }
  
  public CustomLabel()
  {
      InitializeComponent();
  }
  }
  </code></pre>
  <p>Here is the page where I am trying to use the custom control.</p>
  <pre><code>&lt;ContentPage xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
           xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
           x:Class=&quot;DPSReminders.Views.DateTimeTest&quot;
           xmlns:fai=&quot;clr-namespace:FontAwesome&quot;
           xmlns:vm=&quot;clr-namespace:DPSReminders.ViewModels&quot;
           xmlns:controls=&quot;clr-namespace:DPSReminders.Controls&quot;
           xmlns:xct=&quot;http://xamarin.com/schemas/2020/toolkit&quot;
           &gt;
      &lt;controls:CustomLabel LabelText = &quot;{Binding MyLabelText}&quot;/&gt;
  &lt;/ContentPage&gt;
  </code></pre>
  <p>I am wondering if I could do a line like this in my label, that would make my life much easier.</p>
  <pre><code>    &lt;controls:CustomLabel:myLabel.Text = &quot;{Binding MyLabelText}&quot;/&gt;
  </code></pre>
  <p>Then, I can remove all the code creating the BindableProperties and the supporting CLR properties etc. when a built-in bindable property for the same purpose is already available in the child control.  Is this something we can do?</p>