首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListBoxItems背景与ListBox ItemsSource相同

ListBoxItems背景与ListBox ItemsSource相同
EN

Stack Overflow用户
提问于 2015-06-28 05:10:01
回答 1查看 457关注 0票数 0

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

蓝色,红色,绿色。

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

提前谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-28 06:16:04

这可以通过几种方式来实现,具体取决于您的用例。

使用ValueConverters**:** 解决方案1-使用

您可以使用ListBox.ItemContainerStyleListBoxItem进行样式化,然后使用ValueConverterstring值转换为相应的SolidColorBrushes

XAML:

代码语言:javascript
复制
<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>

价值转换器:

代码语言:javascript
复制
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.ItemContainerStyleListBoxItem进行样式化,然后定义几个DataTriggers来根据项的值更改ListBoxItem.Background。这种方法是纯XAML的:

代码语言:javascript
复制
    <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方法:

代码语言:javascript
复制
    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,以将这些名称转换为所需的颜色:

代码语言:javascript
复制
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方法:

代码语言:javascript
复制
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        var colorStr = value.ToString();
        var color = ColorTranslator.FromName(colorStr);
        return new SolidColorBrush(color);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31096352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档