【发布时间】:2018-05-17 06:13:58
【问题描述】:
这是我的 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>
还有我的代码:
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"];
}
// ...
如果我将BGColproperty 更改为字符串类型并在任何地方使用"Orange" 而不是Color.Orange...它仍然不起作用。
如何将我的ControlTemplate 中Grid 控件的背景属性绑定到BGCol?
[忽略] 不相关的文字,因为 SO 还是不开心,说了太多代码。 有时带有内联 cmets 的代码(正如我所做的那样)比任何数量的文本都能更好地解释它,你会认为像 SO 这样的网站会理解这一点,但不。 更多的废话。 更何况。 哦,我们开始吧,这似乎足够了。 [/忽略]
【问题讨论】:
标签: xamarin.forms controltemplate templatebinding