简体   繁体   中英

wpf listbox with colorized items

I have a Listbox which I use as Color Palette. The colors are added with hex code. Hex code is hardly readable for Humans (at least me) which is why I want to colorize the Items in my ListBox.

My Listbox looks like this right now:
在此处输入图像描述

As an example of what I mean, I have a simple TextBox which is beeing colorized: 在此处输入图像描述

System.Drawing.Color? color = cl.ColorFromHexString(Basecolor.Text);
if (color != null)
{
    System.Drawing.Color col = (System.Drawing.Color)color;
    this.Basecolor.Foreground = new SolidColorBrush(cl.ToMediaColor(col));
}

The Listbox Items are assigned by using the following code (probably not best practice):

List<String> ColorStringPalette = new List<string>();
private void AddColorButton_Click(object sender, RoutedEventArgs e)
{
    System.Drawing.Color? color = cl.ColorFromHexString(ColorHexCode.Text);
    if (color != null)
    {
        if (!ColorStringPalette.Contains(ColorHexCode.Text))
        {
            ColorStringPalette.Add(ColorHexCode.Text);
        }
        ColorList.ItemsSource = ColorStringPalette;
    }
}

What would be the apropriate way to set foreground colors for the individual items of the Listbox?

you can change ItemTemplate and bind Foreground (it requires brush but will do converion from string):

<ListBox.ItemTemplate>
    <DataTemplate>
         <TextBlock Text="{Binding}" Foreground="{Binding}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM