首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Windows Mobile >

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

2012-03-23 
textblock某个特定字符的颜色的动态改变想在wp7代码中动态的控制textblock文字的其中某一个字的颜色的改变

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

热点排行