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