【发布时间】:2015-11-30 16:22:16
【问题描述】:
我想自定义我的列表视图以根据标签属性添加一些颜色。
如果我的金额标签 > 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 -> amoutLabel.TextColorProperty = Color.Green 否则 amountLabel.TextColorProperty = Color.Red
我知道我必须使用 TextColorProperty 但如何检索标签的 Text 属性?
【问题讨论】:
标签: c# listview binding xamarin textcolor