读书人

textblock某个特定字符的颜色的动态改

发布时间: 2012-03-23 12:06:21 作者: rapoo

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) }); 

读书人网 >Windows Mobile

热点推荐