根据数据绑定值设置背景颜色

rkc*_*c88 9 xaml binding xamarin xamarin.forms

我以前见过一些答案,但没有什么能真正帮助我.

我也有一个类DecideModel(这将是从DB检索的数据集,但为了这个问题的目的,我添加了一个ObservableCollection),其中包含

static DecideModel()
    {
        All = new ObservableCollection<DecideModel>
        {
            new DecideModel
            {
                DatePerformed = new DateTime(2015, 4, 06),
                Result = "Maybe"
            },
            new DecideModel
            {
                DatePerformed = new DateTime(2015, 4, 05),
                Result = "No"
            },
            new DecideModel
            {
                DatePerformed = new DateTime(2015, 4, 04),
                Result = "Yes"
            }
        };
    }

    public DateTime DatePerformed { set; get; }

    public string  Result { set; get; }

    public static IList<DecideModel> All { set; get; }
}
Run Code Online (Sandbox Code Playgroud)

在我的XAML代码中

<ContentPage.Resources>
    <ResourceDictionary>
        <Color x:Key="Maybe">#ffddbc21</Color>
        <Color x:Key="Yes">#3CB371</Color>
        <Color x:Key="No">#B22222</Color>
        <Color x:Key="Depends">#ffd78800</Color>
    </ResourceDictionary>
</ContentPage.Resources>

<Label Text="{Binding Result}" HorizontalOptions="FillAndExpand" BackgroundColor="{StaticResource {BindingSource Result}}" />
Run Code Online (Sandbox Code Playgroud)

我试图根据从Object获得的结果动态设置标签的背景颜色.

如果您对如何操作有任何想法,请告诉我.我正在寻找任何有用的选项.

Ger*_*uis 16

你可能需要的是一个ValueConverter.你现在正在做的是将背景颜色设置为"可能","否"或"是",这显然不是一种颜色.

您需要做的是将该值转换为颜色.你可以这样做.

创建一个实现该IValueConverter接口的新类.它可能看起来像这样:

public class YesNoMaybeToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
            switch(value.ToString().ToLower())
            {
                    case "yes":
                        return Color.Green;
                    case "no":
                        return Color.Red;
                    case "maybe":
                        return Color.Orange;
            }

            return Color.Gray;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
            // You probably don't need this, this is used to convert the other way around
            // so from color to yes no or maybe
            throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将此类作为静态资源添加到您的XAML页面,如下所示:

<ContentPage.Resources>
   <!-- Add this line below -->
   <local:YesNoToBooleanConverter x:Key="YesNoMaybeToColorConverter" />
   <!-- You can remove the underneath -->
    <!--<ResourceDictionary>
        <Color x:Key="Maybe">#ffddbc21</Color>
        <Color x:Key="Yes">#3CB371</Color>
        <Color x:Key="No">#B22222</Color>
        <Color x:Key="Depends">#ffd78800</Color>
    </ResourceDictionary>-->
</ContentPage.Resources>
Run Code Online (Sandbox Code Playgroud)

现在在绑定中你必须告诉他使用什么转换器.像这样做:

<Label Text="{Binding Result}" HorizontalOptions="FillAndExpand" BackgroundColor="{Binding Result, Converter={StaticResource YesNoMaybeToColorConverter}}" />
Run Code Online (Sandbox Code Playgroud)

它现在应该看到Result字段中的值,将其放入已定义的转换器并返回与该值对应的颜色.


Rud*_*ser 11

对于无需太多开销即可实现此目的的纯 XAML 方式,您可以使用DataTrigger. 请注意,您可以根据需要为每个触发器添加任意数量的 setter,这比之前建议的解决方案稍微灵活一些,它还可以将视图逻辑保留在应有的位置。

<Label Text="{Binding Result}" HorizontalOptions="FillAndExpand">
    <Label.Triggers>
        <DataTrigger TargetType="Label" Binding="{Binding Result}" Value="Yes">
            <Setter Property="BackgroundColor" Value="#3CB371" />
        </DataTrigger>
        <DataTrigger TargetType="Label" Binding="{Binding Result}" Value="No">
            <Setter Property="BackgroundColor" Value="#B22222" />
        </DataTrigger>
        <DataTrigger TargetType="Label" Binding="{Binding Result}" Value="Maybe">
            <Setter Property="BackgroundColor" Value="#ddbc21" />
        </DataTrigger>
        <DataTrigger TargetType="Label" Binding="{Binding Result}" Value="Depends">
            <Setter Property="BackgroundColor" Value="#d78800" />
        </DataTrigger>
    </Label.Triggers>
</Label>
Run Code Online (Sandbox Code Playgroud)

请注意,您可以通过将BackgroundColor属性设置为合理的默认值(在这种情况下可能是“取决于”)来删除其中一个触发器。