我有一个绑定到列表的 StackLayout,我想将每个项目的
Text
属性放入标签中,并根据 IsSpecial
属性以以下方式使用不同的颜色:
<StackLayout BindableLayout.ItemsSource="{Binding Lines}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label Text="{Binding Text}" TextColor= "Red if IsSpecial otherwise White"/>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
我尝试使用触发器,但我不知道如何正确引用目标类型。
由于您没有显示要绑定的模型类,我将做出有根据的猜测:
public class Line
{
public string Text { get; set; }
public bool IsSpecial { get; set; }
}
然后你可以像这样使用DataTrigger:
<StackLayout BindableLayout.ItemsSource="{Binding Lines}">
<BindableLayout.ItemTemplate>
<DataTemplate x:DataType="model:Line">
<Label
Text="{Binding Text}"
TextColor="White">
<Label.Triggers>
<DataTrigger
TargetType="Label"
Binding="{Binding IsSpecial}"
Value="true">
<Setter
Property="TextColor"
Value="Red" />
</DataTrigger>
</Label.Triggers>
</Label>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>