C# ListBoxItems背景与ListBoxItemsSource相同
我有一个WPFC# ListBoxItems背景与ListBoxItemsSource相同,c#,wpf,listbox,bind,listboxitem,C#,Wpf,Listbox,Bind,Listboxitem,我有一个WPF列表框项目资源,它绑定到一个可观察集合。列表框值为: 蓝色, 红色 绿色 我希望该项目的背景颜色匹配它的价值。例如,我希望蓝色项目的背景颜色为蓝色、红色到红色,等等。我无法找到更改每个ListBoxItem的方法,因为我使用的是ItemsSource。如何将ListBoxItems背景色绑定到这些相应的值 提前谢谢你 这可以通过几种方式实现,具体取决于您的用例 解决方案1-使用值转换器: 您可以使用ListBox.ItemContainerStyle设置ListBoxItem的样式
列表框
项目资源
,它绑定到一个可观察集合
。列表框值为:
蓝色,
红色
绿色
我希望该项目的背景颜色匹配它的价值。例如,我希望蓝色项目的背景颜色为蓝色、红色到红色,等等。我无法找到更改每个ListBoxItem
的方法,因为我使用的是ItemsSource
。如何将ListBoxItems
背景色绑定到这些相应的值
提前谢谢你 这可以通过几种方式实现,具体取决于您的用例 解决方案1-使用
值转换器
:
您可以使用ListBox.ItemContainerStyle
设置ListBoxItem
的样式,然后使用ValueConverter
将字符串
值转换为相应的值
solidColorBrush
XAML:
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>
如果您的颜色名称没有特定的格式,并且可以是任何颜色(例如,您的外部文件可能包含“VeryLightGreen”或“PrettyYellow”!),则您应该定义自己的颜色字典
,以将这些名称转换为所需的颜色:
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;
}
}
这是WPF的问题吗?我很抱歉。是的,这是WPF应用程序。您好,谢谢您的时间。我将此标记为一个答案,因为它按原样工作,但我应该在我的作品中更具描述性。那是深夜。颜色实际上是动态加载的,从外部文件读取。如果在ObsColl中加载这些,如何修改转换器?我不能使用开关,因为这是动态完成的。非常感谢。
<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>
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);
}
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;
}
}
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
var colorStr = value.ToString();
var color = ColorTranslator.FromName(colorStr);
return new SolidColorBrush(color);
}