listview xamarin中的标签颜色属性
本文关键字:颜色 属性 标签 xamarin listview | 更新日期: 2023-09-27 18:25:01
我想自定义我的列表视图,根据标签属性添加一些颜色。
如果我的金额标签>0,我想将颜色设置为绿色,如果不设置为红色。
我该怎么做?
// Create the list view
listView = new ListView
{
// Source of data items
ItemsSource = items,
// Define the template for displaying each item
ItemTemplate = new DataTemplate(() =>
{
Label noLabel = new Label();
noLabel.SetBinding(Label.TextProperty, "no");
Label orderDateLabel = new Label();
orderDateLabel.SetBinding(Label.TextProperty,
new Binding("orderDate") {Converter = new DateConverter()});
Label customerNameLabel = new Label();
customerNameLabel.SetBinding(Label.TextProperty, "customerName");
Label externalDocumentNoLabel = new Label();
externalDocumentNoLabel.SetBinding(Label.TextProperty, "externalDocumentNo");
Label amountLabel = new Label();
amountLabel.HorizontalTextAlignment = TextAlignment.End;
// Binding with converter
amountLabel.SetBinding(Label.TextProperty, new Binding("amount") {Converter = new AmountConverter()});
// Return an assembled view cell
return new ViewCell
{
View = new Grid
{
// Fill the grid with data and position
Children =
{
{
noLabel, 0, 0
},
{
orderDateLabel, 1, 0
},
{
customerNameLabel, 2, 0
},
{
externalDocumentNoLabel, 3, 0
},
{
amountLabel, 4, 0
}
}
}
};
})
};
如果amountLabel>0->amountLabel.TextColorProperty=Color.GreenElse amountLabel.TextColorProperty=颜色.红色
我知道我必须处理TextColorProperty,但如何检索标签的Text属性?
将其绑定到与text属性相同的数据元素,并使用转换器将值转换为Color。
amountLabel.SetBinding(Label.TextColorProperty, new Binding("amount")
{ Converter = new AmountColorConverter() }
);