How does foreground color change on only one line of a wpf list?

How does the color of the foreground text (and not the selected text or selection background) in the wpf list change? let's say, for example, I wanted to make all the letters "a" green, all the letters "b" red, etc.? how can i programmatically do this as i add them to c # all I can find is posting people about changing the selected text, I want to change the color of the foreground text so that it looks more organized.

on the side of the note, why does stackoverflow give me problems with this question? says the question is "not up to quality standards." I think this is a perfectly legitimate question. What filter is put to this question that does not meet the standards?

I want to do this:

string[] pics= Directory.GetFiles(@"C:\\", "*.jpg");
        foreach (string pic in pics)
        {
            CHANGE THE FOREGROUND COLOR TO RED
            lbxFileList.Items.Add(pic);
        }
string[] vids= Directory.GetFiles(@"C:\\", "*.mpg");
        foreach (string vid in vids)
        {
            CHANGE THE FOREGROUND COLOR TO GREEN
            lbxFileList.Items.Add(vid);
       }
+3
3

:

<ListBox x:Name="lbxFileList">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel>
         <TextBlock Text="{Binding Path=.}" ForeGround={Binding ., Converter={StaticResource ItemToBrushConverter}}/>
       </StackPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

Brush, :

class FileNameToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
        CultureInfo culture)
    {

        return value.EndsWith("mpg") ? Brushes.Green : Brushes.Red;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
+4

, listboxitems ( ), , .

+2

To build on the above solution:

foreach (string pic in pics)
{
    if (string.IsNullOrEmpty(pic))
        continue;

    string first = pic.Substring(0, 1);
    Color color;

    switch (first.ToLower())
    {
        case "a":
            color = Colors.Green;
            break;
        case "b":
            color = Colors.Red;
            break;
        default:
            color = Colors.Black;
    }

    ListBoxItem item = new ListBoxItem() {
        Content = pic,
        Foreground = new SolidColorBrush(color)
    };

    lbxFileList.Items.Add(pic);
}
+1
source

All Articles