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详解 命令行选项 工具窗口 文档窗口 工具栏 菜单 ...
随机推荐
- YOLO v1之总结篇(linux+windows)
YOLO出自2016 CVPR You Only Look Once:Unified, Real-Time Object Detection,也是一个非常值得学习的框架,不得不说facebook的技术 ...
- 【SYZOJ279】滑稽♂树(树套树)
[SYZOJ279]滑稽♂树(树套树) 题面 SYZOJ CJOJ 题目描述 zzsyz实验楼里面种了一棵滑稽树,只有滑稽之力达到大乘期的oier才能看到.虽然我们看不到,但是还是知道一些信息: 这真 ...
- (转)Xsl 的Webshell(aspx)版本
关于使用xsl的webshell以前已经有人发过了,比如aspx的一个webshell如下: <%@ Page Language="C#" Debug="true& ...
- 《Linux内核设计与实现》学习总结 Chap1~2
第一章 Linux内核简介 一.历史 由于Unix系统设计简洁并且在发布时提供源代码,所以许多其他组织和团体都对它进了进一步的开发. Unⅸ虽然已经使用了40年,但计算机科学家仍然认为它是现存操作系统 ...
- 【loj2461】【2018集训队互测Day 1】完美的队列
#2461. 「2018 集训队互测 Day 1」完美的队列 传送门: https://loj.ac/problem/2461 题解: 直接做可能一次操作加入队列同时会弹出很多数字,无法维护:一个操作 ...
- 【loj2064】找相同字符
Portal --> loj2064 Solution 这里是用后缀数组做的版本!(晚点再用Sam写一遍qwq) 首先一个字符串的子串其实就是这个字符串某个后缀的前缀,所以我们有一个十分简单 ...
- PHP 多线程采集
function curl_multi($urls) { if (!is_array($urls) or count($urls) == 0) { return false; } $num=count ...
- 基于 Quartz.NET 实现可中断的任务
基于 Quartz.NET 实现可中断的任务 Quartz.NET 是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET 允许开发 ...
- GIT每次都要输入用户名和密码的解决方案
三.配置客户端长期存储用户各和密码 长期存储密码: git config --global credential.helper store 缓存用户信息 3600s zb@zb-computer:/h ...
- python---twisted的使用,使用其模拟Scrapy
twisted的网络使用 twisted的异步使用 一:简单使用 from twisted.internet import defer from twisted.web.client import g ...