Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用数据绑定设置背景_C#_Wpf - Fatal编程技术网

C# 使用数据绑定设置背景

C# 使用数据绑定设置背景,c#,wpf,C#,Wpf,我得到了一份动物列表,如下所示: <ListBox ItemsSource="{Binding Source={StaticResource CvsAnimals}}"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <WrapPanel IsItemsHost="True" /> </ItemsPan

我得到了一份动物列表,如下所示:

    <ListBox ItemsSource="{Binding Source={StaticResource CvsAnimals}}">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
我希望根据每个元素的属性
Health
设置每个动物的背景色。但是背景色的设置不起作用。为什么?

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

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;
    }

转换器将运行状况转换为
SolidColorBrush
,因此应如下所示:

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

我也遇到过类似的问题,只需对颜色调用
ToString()
,您就被排序为:-),即Colors.Red.ToString();现在觉得有点傻。你是对的!谢谢你的帮助。@nabulke不客气,在学习WPF的初期,我有时也会想到这一点。
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;
    }
<Border Background="{Binding Health, Converter={StaticResource HealthToColor}}">
    <StackPanel Orientation="Vertical">
        <TextBlock Text="{Binding VisualId}" />
    </StackPanel>
</Border>
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;
}