c# - Xamarin.Forms如何根据条件绑定(bind)背景颜色

标签 c# android ios xaml xamarin

如何根据状态绑定(bind)背景颜色 我尝试了这段代码,但它不起作用

var cat = JsonConvert.DeserializeObject<List<TableStat>>(response);
                for(int i = 0;i<cat.Count;i++)
                {
                    if (cat[i].table_status == "Available")
                    {
                        color = "Green";
                        this.BindingContext = color;
                    }
                    else if (cat[i].table_status == "Unavailable")
                    {
                        color = "Black";
                        this.BindingContext = color;
                    }

                }

我将颜色绑定(bind)到.xaml

 <StackLayout HorizontalOptions="FillAndExpand"  BackgroundColor="{Binding color}">

最佳答案

首先,你只能绑定(bind)到公共(public)属性

public Color BGColor { get; set; }

BindingContext = this;

然后在您的代码中设置该属性的值 - 您可能还需要在您的类上实现 INPC

            for(int i = 0;i<cat.Count;i++)
            {
                if (cat[i].table_status == "Available")
                {
                    BGColor = Color.Green;
                }
                else if (cat[i].table_status == "Unavailable")
                {
                    BGColor = Color.Black;
                }

            }

关于c# - Xamarin.Forms如何根据条件绑定(bind)背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52406244/

相关文章:

android - 在 Sony Android IR SDK 中发送自定义 IR 代码的任何方式

ios - 无法保存 NETunnelProviderManager 的配置

c# - 将此 C# 字典转换为 VB.NET

c# - ImmutableList 和 List 之间的一般性能比较?

c# - 在 .ts 中自动创建 .cs 类定义

c# - 带有可以转售的 API 的 VOIP 服务?

java - 如何从 OpenGL ES 2.0 中的数组中读取超过 4 个顶点数据?

android - 如何将转换字符串写入Document对象

iphone - 输入文本时在 UIAlertView 中启用按钮 - 多个警报 View

ios - 如何记录所有 Realm 数据库访问?