【发布时间】:2023-03-03 23:02:01
【问题描述】:
所以我在 Xamarin 中有一个简单的TableView,其中每一行都连接到一个 ControlTemplate。我想使用ViewCells 中的 ControlTemplate 上的附加属性设置一些属性,但是,无论我尝试什么,TemplateBindings 似乎都忽略了附加属性。
所以这里是 ControlTemplate:
<ControlTemplate x:Key="ResultTemplate">
<StackLayout Margin="20,0,20,0"
BackgroundColor="{TemplateBinding attachableProperties:ColorProperties.BackgroundColor}">
<StackLayout Margin="0,5,0,0">
<ContentPresenter />
<controls:Separator />
</StackLayout>
</StackLayout>
</ControlTemplate>
我使用 ViewCell 内部的以下代码连接到此模板:
<ViewCell>
<ContentView ControlTemplate="{StaticResource ResultTemplate}"
attachableProperties:ColorProperties.BackgroundColor="{StaticResource OddRowBackgroundColor}">
<Label Text="Hi" />
</ContentView>
</ViewCell>
最后是附加属性的代码:
public static readonly BindableProperty BackgroundColorProperty = BindableProperty.CreateAttached(
"BackgroundColor",
typeof(Color),
typeof(ColorProperties),
Color.Red,
propertyChanged: ((bindable, value, newValue) =>
{
System.Diagnostics.Debug.WriteLine(value);
}));
public static Color GetBackgroundColor(BindableObject obj)
{
return (Color) obj.GetValue(BackgroundColorProperty);
}
public static void SetBackgroundColor(BindableObject obj, Color value)
{
obj.SetValue(BackgroundColorProperty, value);
}
我已将Debug.Writeline 添加到propertyChanged 参数以查看它是否被调用,它确实如此。所以我认为问题确实出在 TemplateBinding 上。但是为什么它不起作用,我不明白为什么 TemplateBinding 没有连接到附加的属性。
【问题讨论】:
-
嗨,你有更新吗?
标签: c# xaml xamarin attached-properties templatebinding