[英]WPF listbox with different colors
我有一個列表框有問題,該列表框應在他的項目中使用不同的顏色。
<ListBox i:Name="listBox1" ItemsSource="{Binding MyItems_listbox1}"
IsSynchronizedWithCurrentItem="True">
現在,我想向列表框中添加一個項目,以不同的顏色寫出字符串,例如:
listBox1.Items.Add("hallo my name");
我希望將“ hallo”(藍色)“ my”(紅色)“ name”(綠色)添加到列表框中並顯示。 有一些可能的方法來實現這一目標嗎?
實現此目的的可能方法可能是使用ListBox DataTemplate
,將字符串作為數組發送,然后在DataTemplate
內部,對於字符串的所有部分可以使用不同的TextBlock
<TextBlock Text="hallo " Foreground="Blue" />
<TextBlock Text="my" Foreground="Red" />
<TextBlock Text=" name" Foreground="Green" />
為了突出顯示一些單詞,通常我們可以想到RichTextBox
控件。 但是,這不是輕量級的控件。 幸運的是,WPF支持許多控件(尤其是與Document
相關的控件),使我們能夠顯示富文本格式。 對於輕量級解決方案,您應該只使用一個TextBlock
表示每個ListViewItem
的內容,但是我們可以在每個TextBlock
使用Run
元素來突出顯示單詞。 首先,我們需要使用DataTemplate
為每個ListViewItem
設置ItemTemplate
。 我們必須使用Binding
將(每個項目的)字符串內容Binding
到DataTemplate
內部的TextBlock
。 使用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
)使用背后的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.