Here's my XAML:
<?xml version="1.0" encoding="UTF-8"?>
<TemplatedView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:control="clr-namespace:Core.View.Control"
x:Class="Core.View.Control.MyButton">
<TemplatedView.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="MyButtonTemplate">
<Grid BackgroundColor="Teal">
<!-- other irrelevant stuff. Trust me, it's irrelevant. -->
<Grid x:Name="MyGrid" Grid.Row="1" ColumnSpacing="0" BackgroundColor="{TemplateBinding Parent.BGCol}"> <!-- Can't get this TemplateBinding to work -->
<!-- If I change the above to something static, like BackgroundColor="Orange", it works. -->
</ControlTemplate>
<Style TargetType="TemplatedView" x:Key="MyButtonStyle">
<Setter Property="ControlTemplate" Value="{StaticResource MyButtonTemplate}" />
</Style>
And my code behind:
namespace Core.View.Control
{
public partial class MyButton : TemplatedView
{
public static readonly BindableProperty BGColProperty =
BindableProperty.Create("BGCol", typeof(Color), typeof(MyButton), Color.Orange);
public Color BGCol
{
get
{
return (Color)GetValue(BGColProperty);
}
set
{
SetValue(BGColProperty, value);
}
}
public MyButton()
{
InitializeComponent();
Style = (Style)this.Resources["MyButtonStyle"];
}
// ...
If I change BGColproperty
to be of type string and use "Orange"
instead of Color.Orange
everywhere...it still doesn't work.
How do I bind the background property of the Grid
control in my ControlTemplate
to BGCol
?
[ignore] Irrelevant text because SO is still not happy, saying too much code. Sometimes code with inline comments (as I've done) pretty much explains it better than any amount of text can, and you'd think that a site like SO would understand this, but nah. More blabber. More still. Oh here we go, this seems enough. [/ignore]
<Grid x:Name="MyGrid" BackgroundColor="{TemplateBinding BGCol}" >
Template Bindings go to the control, so the Parent is just confusing it.