I am trying to make some custom ViewCells that have bindable properties in them. However, on the consuming page, if I try to pass in an additional bound value (e.g. to a ViewModel), the value is not passed in. Here is the test code to show the issue:
MainPage.xaml:
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ControlBinding" x:Class="ControlBinding.MainPage">
<TableView Intent="Settings">
<TableSection Title="Stock Cell">
<EntryCell Label="Sample" Text="{Binding Message}" />
</TableSection>
<TableSection Title="Custom Cell">
<local:CustomCell Label="Works" Value="Hello World" />
<local:CustomCell Label="Doesn't Work" Value="{Binding Message}" />
</TableSection>
</TableView>
</ContentPage>
MainPage.xaml.cs:
using Xamarin.Forms;
namespace ControlBinding
{
public partial class MainPage : ContentPage
{
public string Message { get; set; }
public MainPage()
{
InitializeComponent();
Message = "Hello Cruel World!";
BindingContext = this;
}
}
}
CustomCell.xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ControlBinding.CustomCell">
<StackLayout Orientation="Horizontal" Padding="16, 10" HorizontalOptions="Fill">
<Label Text="{Binding Label}" />
<Entry HorizontalOptions="EndAndExpand" Text="{Binding Value}" />
</StackLayout>
</ViewCell>
CustomCell.xaml.cs:
using Xamarin.Forms;
namespace ControlBinding
{
public partial class CustomCell : ViewCell
{
public static readonly BindableProperty LabelProperty = BindableProperty.Create("Label", typeof(string), typeof(CustomCell));
public static readonly BindableProperty ValueProperty = BindableProperty.Create("Value", typeof(string), typeof(CustomCell));
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public CustomCell()
{
InitializeComponent();
BindingContext = this;
}
}
}
Passing in the hard coded "Hello World" works, and is displayed. Passing in "{Binding Message}" results in nothing getting displayed.
Am I missing a binding nuance here?