c# - Xamarin.Forms 如何根据条件绑定(bind)背景颜色
<p><p>如何根据状态绑定(bind)背景颜色
我试过这段代码,它没有工作</p>
<pre><code>var cat = JsonConvert.DeserializeObject<List<TableStat>>(response);
for(int i = 0;i<cat.Count;i++)
{
if (cat.table_status == "Available")
{
color = "Green";
this.BindingContext = color;
}
else if (cat.table_status == "Unavailable")
{
color = "Black";
this.BindingContext = color;
}
}
</code></pre>
<p>我将颜色绑定(bind)到 .xaml </p>
<pre><code> <StackLayout HorizontalOptions="FillAndExpand"BackgroundColor="{Binding color}">
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>首先,您只能绑定(bind)到公共(public)属性</p>
<pre><code>public Color BGColor { get; set; }
BindingContext = this;
</code></pre>
<p>然后在您的代码中,设置该属性的值 - 您可能还需要在您的类上实现 INPC</p>
<pre><code> for(int i = 0;i<cat.Count;i++)
{
if (cat.table_status == "Available")
{
BGColor = Color.Green;
}
else if (cat.table_status == "Unavailable")
{
BGColor = Color.Black;
}
}
</code></pre></p>
<p style="font-size: 20px;">关于c# - Xamarin.Forms 如何根据条件绑定(bind)背景颜色,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/52406244/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/52406244/
</a>
</p>
页:
[1]