I've have a textblock and a textbox, I need to check and whether the textblock contains the textbox value, if yes then that specific value should be highlighted in some color. . .
For example,
TextBlock.Text="Just a Test"
if we type "te" in TextBox, then the value in the textblock should be highlight as "Just a *Te*st"
in xaml.
if anybody know means please say!
Thanks in Advance!
Heres a sample code to be put in the text box TextChange
event
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
string text = "this is a TextBlock";
textBlock1.Text = text;
textBlock1.Inlines.Clear();
int index = text.IndexOf(textBox1.Text);
int lenth = textBox1.Text.Length;
if (!(index < 0))
{
Run run = new Run() { Text = text.Substring(index, lenth), FontWeight = FontWeights.ExtraBold };
run.Foreground = new SolidColorBrush(Colors.Orange);
textBlock1.Inlines.Add(new Run() { Text = text.Substring(0, index), FontWeight = FontWeights.Normal });
textBlock1.Inlines.Add(run);
textBlock1.Inlines.Add(new Run() { Text = text.Substring(index + lenth), FontWeight = FontWeights.Normal });
textBlock1.FontSize = 30;
textBlock1.Foreground = new SolidColorBrush(Colors.White);
}
else
{
textBlock1.Text = "No Match";
}
}
this will highlight the text and return no match is no match is found.
note : this code snippet is case sensitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With