Первый : TextBlock может состоять из серии элементов Inline
, каждый из которых может иметь различные характеристики шрифта. Попробуйте посмотреть на результаты этого: -
<TextBlock><Run FontWeight="Bold">Sy</Run><Run>stem</Run></TextBlock>
Секунда : Вы можете связать ListBox
с ObservableCollection<TextBlock>
, если только один раз.
Третий : Вы можете управлять содержимым набора Inlines
текстового блока в коде.
Собираем все вместе: -
Xaml: -
<UserControl x:Class="StackoverflowSpikes.ItemHighlighter"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox Name="textBox1" TextChanged="textBox1_TextChanged" />
<ListBox Grid.Row="1" Name="listBox1" />
</Grid>
</UserControl>
Код: -
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Collections.ObjectModel;
namespace StackoverflowSpikes
{
public partial class ItemHighlighter : UserControl
{
ObservableCollection<TextBlock> items = new ObservableCollection<TextBlock>();
string[] source = new string[] { "Hello", "World", "System", "SystemDefault", "SystemFolder" };
public ItemHighlighter()
{
InitializeComponent();
Loaded += new RoutedEventHandler(ItemHighlighter_Loaded);
}
void ItemHighlighter_Loaded(object sender, RoutedEventArgs e)
{
foreach (string s in source)
{
TextBlock item = new TextBlock();
item.Inlines.Add(new Run() { Text = "", FontWeight = FontWeights.Bold });
item.Inlines.Add(new Run() { Text = s });
items.Add(item);
}
listBox1.ItemsSource = items;
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
string match = textBox1.Text;
foreach (TextBlock item in listBox1.Items)
{
Run bold = ((Run)item.Inlines[0]);
Run normal = ((Run)item.Inlines[1]);
string s = bold.Text + normal.Text;
if (s.StartsWith(match))
{
bold.Text = s.Substring(0, match.Length);
normal.Text = s.Substring(match.Length);
}
else
{
bold.Text = "";
normal.Text = s;
}
}
}
}
}
Вставьте это в новый проект и поиграйте. Должны работать SL3 и 4.