Silverlight如何高亮搜索的关键字

尝试了许多方法之后,发现都不支持Binding的方式来高亮搜索的关键字,但是这对于提高用户体验来说是至关重要的。下面使用TextBlock作为显示内容的容器,对所要显示的String源做一层转换即可。

View Code
 1  private void HighLightString(TextBlock tb, string input, string term)
2 {
3 tb.Inlines.Clear();
4 var returnString = input.Replace(term, string.Format("|{0}|", term));
5 var strings = returnString.Split('|').ToList();
6 foreach (var item in strings)
7 {
8 Run run = new Run()
9 {
10 Text = item.ToString() == term ? term : item.ToString(),
11 Foreground = item.ToString() == term ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Black)
12 };
13
14 tb.Inlines.Add(run);
15 }
16 }



你可能感兴趣的:(silverlight)