Xamarin.forms 为什么';t我的模板绑定到我的自定义模板视图';s可绑定属性工作

Xamarin.forms 为什么';t我的模板绑定到我的自定义模板视图';s可绑定属性工作,xamarin.forms,controltemplate,templatebinding,Xamarin.forms,Controltemplate,Templatebinding,这是我的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.Con

这是我的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>
如果我将
BGColproperty
更改为string类型,并使用
“Orange”
而不是
Color.Orange
无处不在…它仍然不起作用

如何将我的
ControlTemplate
Grid
控件的后台属性绑定到
BGCol

[忽略] 因为不相关的文字所以还是不开心,说的代码太多。 有时,带有内联注释的代码(正如我所做的)比任何数量的文本都能更好地解释它,你可能会认为这样的网站会理解这一点,但不是。 更多的废话。 更多。 哦,我们走了,这似乎足够了。 [/ignore]


模板绑定转到控件,因此父控件只是混淆了它

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"];
    }

    // ...
<Grid x:Name="MyGrid" BackgroundColor="{TemplateBinding BGCol}" >