WPF列表框具有不同的颜色

时间:2014-07-18 06:50:42

标签: c# wpf listbox

我的列表框有问题,应该在他的项目中计算不同的颜色。

    <ListBox i:Name="listBox1" ItemsSource="{Binding MyItems_listbox1}" 
     IsSynchronizedWithCurrentItem="True">

现在我想在列表框中添加一个项目,如果字符串是用不同的颜色写的,例如:

listBox1.Items.Add("hallo my name");

我希望&#34;你好&#34;(蓝色)&#34;我的&#34;(红色)&#34;名称&#34;(绿色)将被添加到列表框并显示。 有没有办法实现这个???

2 个答案:

答案 0 :(得分:1)

实现此功能的可能方法可能是使用ListBox DataTemplate将字符串作为数组发送,然后在DataTemplate内部,您可以为字符串的所有部分设置不同的TextBlock

<TextBlock Text="hallo " Foreground="Blue" />
<TextBlock Text="my" Foreground="Red" />
<TextBlock Text=" name" Foreground="Green" />

答案 1 :(得分:1)

要突出某个词,通常我们可以想到RichTextBox控件。然而,它不是轻量级控制。幸运的是,WPF支持许多控件(特别是与Document相关),允许我们显示富文本。对于轻量级解决方案,您应该使用代表每个TextBlock内容的ListViewItem,但我们可以使用每个Run中的TextBlock元素来突出显示这些字词。首先,我们需要使用DataTemplate为每个ItemTemplate设置ListViewItem。我们必须使用Binding将字符串内容(每个项目)绑定到TextBlock内的DataTemplate。使用Binding可以让我们在Converter中注入一些自定义代码。这是详细信息代码:

代码

//The main window class
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        //Init the Keywords first
        Keywords.Add("hallo", Brushes.Blue);
        Keywords.Add("my", Brushes.Red);
        Keywords.Add("name", Brushes.Green);

        //Add some items to the ListView
        lv.Items.Add("hallo my name");
        lv.Items.Add("hello my name");
        lv.Items.Add("goodbye your name");            
    }
    //This dictionary used to hold your keywords corresponding to their Brush
    public static Dictionary<string, Brush> Keywords = new Dictionary<string,Brush>();
}

//The converter class
public class InlinesConverter : IValueConverter
{
    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var content = Convert.ToString(value);
        var dict = MainWindow.Keywords;
        var outString = "<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"  xml:space=\"preserve\">";
        foreach(var word in content.Split(' ')){
            var converted = word;
            Brush fg;
            if (dict.TryGetValue(word, out fg)) {
                var run = new Run(word);
                run.Foreground = fg;
                converted = System.Windows.Markup.XamlWriter.Save(run);
            }
            outString += converted + " ";
        }
        outString += "</TextBlock>";            
        return System.Windows.Markup.XamlReader.Parse(outString);            
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

<强> XAML

<Window x:Class="WpfApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SO3" Height="300" Width="300"
    xmlns:local="clr-namespace:WpfApplication"
    >
  <Window.Resources>
    <local:InlinesConverter x:Key="inlinesConverter"/>
  </Window.Resources>
  <Grid>
    <ListView Name="lv">            
        <ListView.ItemTemplate>
            <DataTemplate>                    
                <ContentControl FontSize="20">
                   <Binding Converter="{StaticResource inlinesConverter}"/>
                </ContentControl>
            </DataTemplate>
        </ListView.ItemTemplate>            
    </ListView>
  </Grid>
</Window>

请注意这里的命名空间,在本演示中,我使用了默认的命名空间WpfApplication。如果您的不同,则应在XAML代码中对其进行编辑。另请注意,如果在XAML代码中添加项目,则会忽略ItemTemplate。我们必须通过Items.Add或数据绑定(使用ItemsSource)来使用代码。

enter image description here

相关问题