背景颜色的值转换器 [英] ValueConverter for Background Color

查看:22
本文介绍了背景颜色的值转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预测类.字段类型之一是枚举:

I have a Forecast class. One of the field's type is Enum:

enum GeneralForecast
{
    Sunny,
    Rainy,
    Snowy,
    Cloudy,
    Dry
}

class Forecast
{
    public GeneralForecast GeneralForecast { get; set; }
    public double TemperatureHigh { get; set;  }
    public double TemperatureLow { get; set; }
    public double Percipitation { get; set; }
}

我在 ListBox 上显示预测列表,我想根据 GeneralForecast 设置 ListBox 中项目的 BackgroundColor.

I display list of forecasts on the ListBox and I want to set the BackgroundColor of the item in the ListBox depend on GeneralForecast.

所以我创建了转换器:

类 GeneralForecastToBrushConverter : IValueConverter{

class GeneralForecastToBrushConverter : IValueConverter {

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var gf = (GeneralForecast) value;
        switch (gf) 
        {
            case GeneralForecast.Cloudy:
                return "FF1D1D1D";
            case GeneralForecast.Dry:
                return "55112233";
            case GeneralForecast.Rainy:
                return "88FF5522";
            case GeneralForecast.Snowy:
                return "9955FF22";
            case GeneralForecast.Sunny:
                return "FF11FF99";
        }
        return "FFFFFFFF";
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

这是我的 XAML:

<Page.Resources>
    <local:GeneralForecastToBrushConverter x:Key="gf2color"/>
</Page.Resources>

<ListBox ItemsSource="{Binding}" Grid.Row="2" HorizontalAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border Margin="4" BorderBrush="Black" Padding="4" 
                        BorderThickness="2" 
                        Background="{Binding GeneralForecast, Converter={StaticResource gf2color}}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontSize="20" FontWeight="Bold" 
                                   Text="{Binding GeneralForecast}"/>                            
                    </StackPanel>
                </Border>
            </DataTemplate>                
        </ListBox.ItemTemplate>
    </ListBox>

如果我调试我的转换器,我可以看到它返回不同的颜色,但我的所有项目都是相同的颜色.为什么?

If I debug my Converter I can see that it returns different colors, but I have all items the same color. Why ?

推荐答案

当您在 XAML 中编写类似的内容时:

When you write something like this in XAML:

<Button Background="#FF11111" />

Xaml 解析器会在运行时将该字符串转换为其等效颜色.

The Xaml parser will convert that string to its equivalent color at runtime.

但是当您在 C# 中以某种方式分配颜色时,您可能不会将颜色设置为字符串.

But when you assign color in C# somehow, you may not set the color as string.

相反,您应该使用类似 SolidColorBrush 的实例.

Instead you should use something like SolidColorBrush instances.

因此返回一些变量,即 Brush,例如纯色或渐变色画笔.

So return some variable which is Brush, such as solid or gradient color brushes.

如果需要任何其他信息,请告诉我.

Let me know if any other information is needed.

这篇关于背景颜色的值转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆