用数据绑定设置背景

时间:2015-09-29 12:57:25

标签: c# wpf

我得到了一份像这样的动物名单:

    <ListBox ItemsSource="{Binding Source={StaticResource CvsAnimals}}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>

每个动物的呈现由DataTemplate定义。模板应设置每个Animal的背景颜色:

<DataTemplate DataType="{x:Type model:Animal}">
    <Border>
        <Border.Background>
            <SolidColorBrush Color="{Binding Health, Converter={StaticResource HealthToColor}}" />
        </Border.Background>
        <StackPanel Orientation="Vertical">
            <TextBlock Text="{Binding VisualId}" />
        </StackPanel>
    </Border>
</DataTemplate>

动物

public enum HealthStatus
{
    Ok,
    Warning,
    Sick,
    Unknown
}

partial class Animal :  ObservableObject
{
    private HealthStatus _health = HealthStatus.Unknown;

    public HealthStatus Health
    {
      get {return _health; }
      set {  Set(() => Health, ref _health, value); }
    }
}

我希望根据每个元素的属性Health设置每个Animal的背景颜色。但是背景颜色的设置不起作用。为什么呢?

这是我的色彩转换器的一部分:

public class HealthColorConverter : IValueConverter
{
    private static readonly SolidColorBrush ColorSick = new SolidColorBrush(Colors.Red);
    private static readonly SolidColorBrush ColorHealthy = new SolidColorBrush(Colors.LimeGreen);
    private static readonly SolidColorBrush ColorWarning = new SolidColorBrush(Colors.Yellow);

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is HealthStatus)
        {
            var health = (HealthStatus)value;

            switch (health)
            {
                case HealthStatus.Ok:
                    return ColorHealthy;
                case HealthStatus.Sick:
                    return ColorSick;
                case HealthStatus.Warning:
                    return ColorWarning;
                default:
                    return ColorSick;
                    //return DependencyProperty.UnsetValue;
            }
        }

        return value;
    }

1 个答案:

答案 0 :(得分:2)

您的转换器会将运行状况转换为SolidColorBrush,因此它应该是这样的:

<Border Background="{Binding Health, Converter={StaticResource HealthToColor}}">
    <StackPanel Orientation="Vertical">
        <TextBlock Text="{Binding VisualId}" />
    </StackPanel>
</Border>

如果保持XAML代码不变,则需要修改转换器以返回Color

public class HealthColorConverter : IValueConverter
{
  private static readonly Color ColorSick = Colors.Red;
  private static readonly Color ColorHealthy = Colors.LimeGreen;
  private static readonly SolidColorBrush ColorWarning = Colors.Yellow;

  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    if (value is HealthStatus)
    {
        var health = (HealthStatus)value;

        switch (health)
        {
            case HealthStatus.Ok:
                return ColorHealthy;
            case HealthStatus.Sick:
                return ColorSick;
            case HealthStatus.Warning:
                return ColorWarning;
            default:
                return ColorSick;
                //return DependencyProperty.UnsetValue;
        }
    }

    return value;
}