Here's a very simple method to highlight a certain word across a tex block's text:
void HighlightInnerText(TextBlock control, string HighlightingKey)
{
//Looping through words after splitting them with an empty character.
string[] strArr = control.Text.Split(' ');
//Clearing the Inlines list of the textBlock.
control.Inlines.Clear();
for (int i = 0; i < strArr.Length; i++)
{
Run run = new Run();
if (strArr[i].ToLower() == HighlightingKey.ToLower())
{
//Define a Run to wrap the keyword and add it to the textBlock.
run.Text = HighlightingKey + " ";
run.FontWeight = FontWeights.Bold;
run.Foreground = new SolidColorBrush(Colors.Red);
}
else
{
//Define an Inline and add the text to it directly.
if (i == strArr.Length)
{
run.Text = strArr[i];
}
else
run.Text = strArr[i] + " ";
}
control.Inlines.Add(run);
}
}
Now we use it in a very simple and a clear way:
HighlightInnerText(_yourTextBlock, "Keyword");
I hope that helps, silver geeks :) ... Let me know if it helped.
No comments:
Post a Comment