SilverLight高亮显示文本
文笔不好,就长话短说,就是想实现这样的效果,比如在成都二环路南一段一号附一号凤舞九天网吧 ,搜索 二环路 九天网吧 然后结果中高亮显示。

<local:TextBlockHighLight TextSource="成都二环路南一段一号附一号凤舞九天网吧" SearchText="二环路 九天网吧 " FontSize="12" FontFamily="Comic Sans MS" Margin="10"/>
代码如下:
public partial class TextBlockHighLight : UserControl
{
public TextBlockHighLight()
{
InitializeComponent();
this.LayoutRoot.Children.Add(_txtBlock);
}
public string TextSource
{
get { return (string)GetValue(TextSourceProperty); }
set { SetValue(TextSourceProperty, value); }
}
private TextBlock _txtBlock = new TextBlock() { TextWrapping = TextWrapping.Wrap };
// Using a DependencyProperty as the backing store for TextSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextSourceProperty =
DependencyProperty.Register("TextSource", typeof(string), typeof(TextBlockHighLight), new PropertyMetadata(string.Empty, (o, e) =>
{
var _this = o as TextBlockHighLight;
if (string.IsNullOrWhiteSpace(e.NewValue.ToString()))
{
_this._txtBlock.Text = string.Empty;
}
else
{
_this._txtBlock.Text = e.NewValue.ToString();
}
})); public string SearchText
{
get { return (string)GetValue(SearchTextProperty); }
set { SetValue(SearchTextProperty, value); }
} // Using a DependencyProperty as the backing store for SearchText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SearchTextProperty =
DependencyProperty.Register("SearchText", typeof(string), typeof(TextBlockHighLight), new PropertyMetadata(string.Empty, (o, e) =>
{
var _this = o as TextBlockHighLight;
///如果字符都不为空
if ((!string.IsNullOrWhiteSpace(e.NewValue.ToString()))
&& (!string.IsNullOrWhiteSpace(_this._txtBlock.Text)))
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">");
sb.Append(" <TextBlock.Inlines>");
sb.Append("<Run>"); var strs = e.NewValue.ToString().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
if (strs.Length > )
{
///移除相同的 比如二环路 二环 那么会移除二环
for (int i = ; i < strs.Length; i++)
{
for (int j = i + ; j < strs.Length; j++)
{
if (strs[j].Contains(strs[i]))
{
///无效的就设为空字符串
strs[i] = string.Empty;
break;
}
}
}
} ///去除空字符串
strs = strs.Where(c => !string.IsNullOrEmpty(c)).ToArray(); var tmpSB = new System.Text.StringBuilder(); foreach (var item in strs)
{
if (tmpSB.Length == )
{
///设置要高亮显示的样式 ,这里是测试就写死了
tmpSB.Append(_this.TextSource.Replace(item, "</Run><Run Text=\"" + item + "\" Foreground=\"Red\" FontWeight=\"Bold\"></Run> <Run>"));
}
else
{
tmpSB.Replace(item, "</Run><Run Text=\"" + item + "\" Foreground=\"Red\" FontWeight=\"Bold\"></Run> <Run>");
}
}
sb.Append(tmpSB.ToString());
sb.Append("</Run>");
sb.Append("</TextBlock.Inlines>");
sb.Append("</TextBlock>"); var txt = System.Windows.Markup.XamlReader.Load(sb.ToString()) as TextBlock; List<Inline> inlines = new List<Inline>();
txt.Inlines.ToList().ForEach(c => inlines.Add(c)); txt.Inlines.Clear();
_this._txtBlock.Text = null;
foreach (var item in inlines)
{
_this._txtBlock.Inlines.Add(item);
}
_this.UpdateLayout();
}
else
{
_this._txtBlock.Inlines.Clear();
_this._txtBlock.Text = _this.TextSource;
}
}));
}
有些BUG,如果先更新要搜索的字符串,再更新源字符串不会触发事件,但是在实际使用中也是在源字符串里面找要搜索的字符串。搜索字符串里面是加空格分隔的。应该有更好的算法,也可以用正则匹配就用像我代码里面用 XamlReader.Load() ,但是在最先实现中,由于本生自己正则不是好好,所以希望大家提出更好的算法。
SilverLight高亮显示文本的更多相关文章
- HTML5基础-Mark标签高亮显示文本
1.mark标签使用 <mark></mark> 2.mark作用 使用mark标签元素,可以高亮显示文档中的文字以达到醒目的效果. 3.mark使用代码 <!DOCTY ...
- Silverlight中文本框添加回车事件后,换行无法清除的解决方法
在开发Silverlight的项目中,为了更好的用户体验,我们常要给一些控件添加一些快捷键.然而,在Silverlight中当用户回车提交后,光标停留在文本框的第二行怎么也清除不掉,经过一段时间研究, ...
- vim高亮显示文本
行列高亮设置 • 行高亮 " 设置高亮行的颜色,ctermbg设定背景色,ctermfg设定前景色 set cursorline hi CursorLine cterm=NONE cterm ...
- Android 高亮显示文本中的关键字
总结:SpannableString用好,可以各种替换Span来操作各种内容 1.文本关键字高亮关键在于:SpannableString使用 主要就是通过关键字在文本的开始index,结束index来 ...
- silverlight 富文本
- linux中高亮显示文本的工具 -- bat
bat 的项目地址 https://github.com/sharkdp/bat bat 是用rust 开发的, 在centos中安装bat需要rust的环境, 我们可以通过安装rust的包管理工具c ...
- 【WP8】让TextBox文本支持滑动(Scroll)
通过修改样式让TextBox支持文本滑动 在Silverlight上,TextBox是有文本滚动的功能的,当TextBox的文本过长时,可以进行拖动的,TextBox使用 VerticalScroll ...
- Qt Assistant介绍
简介 Qt Assistant也就是我们常说的Qt助手,是一款用于呈现在线文档的工具. 简介 一分钟学会使用 Qt参考文档 Qt Assistant详解 命令行选项 工具窗口 文档窗口 工具栏 菜单 ...
- 【Qt】Qt Assistant介绍【转】
简介 Qt Assistant也就是我们常说的Qt助手,是一款用于呈现在线文档的工具. 简介 一分钟学会使用 Qt参考文档 Qt Assistant详解 命令行选项 工具窗口 文档窗口 工具栏 菜单 ...
随机推荐
- DjangoORM字段参数介绍
参数介绍: 字段的参数: null: ->db是否可以为空 default: ->默认值 primary_key: ...
- Qt消息机制和事件
Qt消息机制和事件 1 事件 事件(event)是由系统或者 Qt 本身在不同的时刻发出的.当用户按下鼠标.敲下键盘,或者是窗口需要重新绘制的时候,都会发出一个相应的事件.一些事件在对用户操作做出响应 ...
- splay tree 学习笔记
首先感谢litble的精彩讲解,原文博客: litble的小天地 在学完二叉平衡树后,发现这是只是一个不稳定的垃圾玩意,真正实用的应有Treap.AVL.Splay这样的查找树.于是最近刚学了学了点S ...
- android adb devices 后出现offline问题的解决
Android在使用adb 命令时有时会发现设备的状态是offline,我的神呀,这怎么让我们调试呢.必须解决这个问题. 解决方式如下: 以下方法请确保你的设备处于调试模式下. 方法一:执行 #adb ...
- selenium测试 - open Firefox
环境:Python2.7+selenium3+Firefox47 问题1: 在打开火狐浏览器时报错:‘geckodriver‘ executable needs to be in PATH fro ...
- Servlet3.0 新特性
Servlet3.0 的注解 Servlet 允许开发人员采用注解的方式来配置 Servlet.Filter.Listener. Servlet3.0 规范在 javax.servlet.annota ...
- python基础5--模块
模块 一.模块简介 模块是一个包含有定义的函数和变量的文件,其后缀名是.py.Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持. 标准 ...
- packet32 -- 打印packet32捕获的包
void PrintPackets(LPPACKET lpPacket) { ULONG i, j, ulLines, ulen, ulBytesReceived; char *pChar, *pLi ...
- 前端PHP入门-032-异常处理-应用级别
禁止显示错误 在php.ini配置文件中.我们可以控制php的错误显示状态. php.ini中有一个专门的配置项: display_errors 这个选项设置是否将错误信息输出到网页,或者对用户隐藏而 ...
- libiconv的注意项
编译后有用的头文件zlib.h和zconf.h,使用时#include "zlib.h". 其中有三个核心的函数: iconv_ticonv_open(constchar*toco ...