列表视图 xamarin 中的标签颜色 属性

Label color property in listview xamarin

我想自定义列表视图以根据标签添加一些颜色 属性。

如果我的金额标签 > 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,但如何检索标签的文本 属性?

您将它绑定到与文本相同的数据元素 属性,并使用转换器将值转换为颜色。

amountLabel.SetBinding(Label.TextColorProperty, new Binding("amount")
   { Converter = new AmountColorConverter() }
);