我有一个WPF ListBox ItemsSource,它绑定到ObservableCollection<string>。列表框值为:
蓝色,红色,绿色。
我希望这个项目的背景色与它的价值相匹配。例如,我希望蓝色项目的背景颜色是蓝色,红色到红色,等等。我无法找到改变每个ListBoxItem的方法,因为我使用的是ItemsSource。如何将ListBoxItems背景色绑定到这些相应的值?
提前谢谢!
发布于 2015-06-28 06:16:04
这可以通过几种方式来实现,具体取决于您的用例。
使用ValueConverters**:** 解决方案1-使用
您可以使用ListBox.ItemContainerStyle对ListBoxItem进行样式化,然后使用ValueConverter将string值转换为相应的SolidColorBrushes。
XAML:
<ListBox ItemsSource="{Binding MyObservableCollection}" >
<ListBox.Resources>
<local:ColorConverter x:Key="converter"/>
</ListBox.Resources>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="{Binding Path=.,
Converter={StaticResource converter}}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>价值转换器:
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
var color = value.ToString();
switch (color)
{
case "Blue":
return new SolidColorBrush(Colors.Blue);
case "Red":
return new SolidColorBrush(Colors.Red);
case "Green":
return new SolidColorBrush(Colors.Green);
}
return SystemColors.ControlColor;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}使用DataTriggers**:** 解决方案2
同样,您可以使用ListBox.ItemContainerStyle对ListBoxItem进行样式化,然后定义几个DataTriggers来根据项的值更改ListBoxItem.Background。这种方法是纯XAML的:
<ListBox ItemsSource="{Binding MyObservableCollection}" >
<ListBox.Resources>
<System:String x:Key="red">Red</System:String>
<System:String x:Key="green">Green</System:String>
<System:String x:Key="blue">Blue</System:String>
</ListBox.Resources>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<DataTrigger Binding="{Binding }" Value="{StaticResource red}">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding }" Value="{StaticResource green}">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding }" Value="{StaticResource blue}">
<Setter Property="Background" Value="Blue"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>编辑:
假设动态加载的颜色名称与System.Windows.Media.Colors的名称相同,那么您可以在第一个解决方案中用以下方法替换Convert方法:
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
var colorStr = value.ToString();
var color = (Color)System.Windows.Media.ColorConverter.ConvertFromString(colorStr);
return new SolidColorBrush(color);
}如果您的颜色名称不遵循特定的格式,并且它们可以是任何内容(即您的外部文件可以包含"VeryLightGreen“或”PrettyYellow“之类的名称!)然后,您应该定义自己的ColorDictionary,以将这些名称转换为所需的颜色:
public static class ColorTranslator
{
private static Dictionary<string, Color> ColorDictionary = LoadColors();
public static Color FromName(string name)
{
return ColorDictionary[name];
}
private static Dictionary<string, Color> LoadColors()
{
var dictionary = new Dictionary<string, Color>();
dictionary.Add("Red", Colors.Red);
dictionary.Add("Blue", Colors.Blue);
dictionary.Add("Green", Colors.Green);
dictionary.Add("VeryLightGreen", Colors.Honeydew);
dictionary.Add("PrettyYellow", Color.FromArgb(200,255,215,0));
return dictionary;
}
}Convert方法:
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
var colorStr = value.ToString();
var color = ColorTranslator.FromName(colorStr);
return new SolidColorBrush(color);
}https://stackoverflow.com/questions/31096352
复制相似问题