ListBoxItems背景与ListBoxItemsSource相同
本文关键字:相同 ListBoxItemsSource 背景 ListBoxItems | 更新日期: 2023-09-27 18:00:13
我有一个绑定到ObservableCollection<string>
的WPF ListBox
ItemsSource
。列表框值为:
蓝色,红色绿色
我希望项目的背景颜色与它的值相匹配。例如,我希望蓝色项目的背景颜色为蓝色、红色到红色,依此类推。我无法找到更改每个ListBoxItem
的方法,因为我使用的是ItemsSource
。如何将ListBoxItems
背景颜色绑定到这些相应的值?
提前感谢!!!
这可以通过几种方式实现,具体取决于您的用例。
解决方案1-使用ValueConverters
:
您可以使用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();
}
}
Soultion 2-使用DataTriggers
:
同样,您可以使用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);
}