【问题标题】:Label color property in listview xamarinlistview xamarin中的标签颜色属性
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    • 如果我更新收藏时我的金额发生变化,绑定是否有效(在我的情况下会改变颜色)?
    • 如果您使用的是 INotifyPropertyChanged,应该这样做
    猜你喜欢
    • 2020-10-29
    • 2021-08-28
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 2021-12-05
    • 2019-08-12
    • 2016-11-03
    • 1970-01-01
    相关资源
    最近更新 更多