c# - 期待xamarin Box-view颜色动态变化

标签 c# xaml xamarin xamarin.forms

我已将框颜色与值(在 View 模型中)和 xaml 绑定(bind),如下所示 在 xaml 中

<BoxView  Grid.Row="2" Color="{Binding ParentContext.BoColor,Source={x:Reference dashboardThree}}" />

在模型 View 中如下

   public string BoColor { get; set; } = "#000080";

所以上面的代码每次运行时都会显示蓝色, 问题是我在 ListView 中动态创建了 10 个框,所有 10 个框都显示了上述颜色(因为我只有 1 个 ListView 来显示我的 jason 对象,它动态创建了 10,15 个等框),所以是他们有什么办法可以为每个不同的盒子添加不同的 10 种颜色(随机或定义的六色)??预先感谢您的支持。

最佳答案

这实际上取决于您是否需要这些颜色对于每个 BoxView 都是可重复的。

这里是一个随机分配颜色的例子。

Random random = new Random();
public string BoColor
{
    get
    {
        return String.Format("#{0:X6}", random.Next(0x1000000));
    }
}

例子:

<StackLayout>
    <Label Text="StackOverflow" BackgroundColor="{Binding BoColor}"/>
    <Label Text="StackOverflow" BackgroundColor="{Binding BoColor}"/>
    <Label Text="StackOverflow" BackgroundColor="{Binding BoColor}"/>
    <Label Text="StackOverflow" BackgroundColor="{Binding BoColor}"/>
    <Label Text="StackOverflow" BackgroundColor="{Binding BoColor}"/>
</StackLayout>

enter image description here

关于c# - 期待xamarin Box-view颜色动态变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51890102/

相关文章:

c# - 如何在 C# 代码中正确使用 PowerShell 模块

c# - 用户定义的后缀

c# - 使用 C# 打印文件 Excel

c# - 将 contextMenu 绑定(bind)到与 TreeView 不同的 View 模型

android - 无法在 xamarin MVVMCROSS 中从 android.support.v4.app.Fragment 转换为 android.app.Fragment

c# - 尝试在 Unity 中使用 Xamarin.iOS 制作的库

c# - 使代码中的大数字可读

c# - ListView 单击时选择和取消选择(无需 Ctrl + 单击)

wpf - 如何从 WPF 中的另一个窗口绑定(bind)到 MainWindow 中的控件?

c# - 在 UWP 中设置自定义 WebView header