ListBoxItems背景与ListBox ItemsSource相同

时间:2015-06-28 05:10:01

标签: c# wpf listbox bind listboxitem

我有一个绑定到ListBox的WPF ItemsSource ObservableCollection<string>。列表框值为:

蓝, 红色, 绿色。

我希望Item的背景颜色与其值相匹配。例如,我希望Blue Item的背景颜色为蓝色,红色为红色等等。我找不到改变每个ListBoxItem的方法,因为我使用ItemsSource。如何将ListBoxItems背景颜色绑定到这些相应的值?

提前谢谢!!!

1 个答案:

答案 0 :(得分:0)

根据您的使用情况,这可以通过几种方式实现。

解决方案1 ​​ - 使用ValueConverters

您可以使用ListBox.ItemContainerStyleListBoxItem设置样式,然后使用ValueConverterstring值转换为相应的值  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.ItemContainerStyleListBoxItem设置样式,然后根据项目的值定义多个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的名称相同,那么您可以用第1个版本替换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);
    }

如果您的颜色名称没有特定格式,并且它们可以是任何内容(即您的外部文件可能包含&#34; VeryLightGreen&#34;或&#34; PrettyYellow&#34;!)等名称,那么您应该定义自己的颜色名称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);
    }