简体   繁体   English

仅在 wpf 列表框的一行上如何更改前景色?

[英]how is the foreground color changed on just one line of wpf listbox?

how is the foreground text color changed (not the selected text, or the background of the selection) in a wpf listbox? wpf 列表框中的前景文本颜色如何更改(不是所选文本或所选背景)? say, for example, i wanted to make all the letter "a" items green, all the letter "b" items red, etc?比如说,我想让所有字母“a”的项目变成绿色,所有的字母“b”项目变成红色,等等? how can i programmatically do that as i add them in c#?我如何以编程方式将它们添加到 c# 中? all i can find is people posting about changing selected text, i want to change the color of the foreground text to make it look more organized.我能找到的只是人们发布有关更改所选文本的信息,我想更改前景文本的颜色以使其看起来更有条理。

on a side note, why is stackoverflow giving me problems about this question?在旁注中,为什么stackoverflow会给我这个问题的问题? says the question "doesn't meet quality standards".说这个问题“不符合质量标准”。 i think this is a perfectly legitimate question.我认为这是一个完全合理的问题。 what filter is put on this question that makes it not meet any standards?什么过滤器放在这个问题上,使它不符合任何标准?

i'm looking 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);
       }

Use a template in combination with a converter:将模板与转换器结合使用:

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

The converter should convert your Item to a Brush that has the color you want:转换器应该将您的项目转换为具有您想要的颜色的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();
    }
}

I agree with the previous answer, but you could also add listboxitems to your listbox (instead of strings), that way you could change the foreground color before adding it to the listbox.我同意前面的答案,但您也可以将 listboxitems 添加到列表框(而不是字符串),这样您就可以在将前景色添加到列表框之前更改前景色。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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