当前位置: 代码迷 >> Windows Mobile >> textblock某个特定字符的颜色的动态改变解决方法
  详细解决方案

textblock某个特定字符的颜色的动态改变解决方法

热度:30   发布时间:2016-04-25 07:43:33.0
textblock某个特定字符的颜色的动态改变
想在wp7代码中动态的控制textblock文字的其中某一个字的颜色的改变
例如文字为“Hello World”想把其中的“o”都改变成红色,应该怎么做呢?

------解决方案--------------------
有一点点复杂
C# code
string text = textBlock1.Text;            textBlock1.Inlines.Clear();            int index, start = 0;            char c = 'o'; //要替换的字符            while ((index = text.IndexOf(c, start)) != -1)            {                textBlock1.Inlines.Add(new Run() {Text = text.Substring(start, index-start)});                textBlock1.Inlines.Add(new Run() {Text = c.ToString(), Foreground = new SolidColorBrush(Colors.Red)});                start = index + 1;            }            if (start < text.Length)                textBlock1.Inlines.Add(new Run() {Text = text.Substring(start) });
  相关解决方案