在文本块中突出显示搜索词
我在 silverlight 应用程序上有一个搜索文本框,用户在其中输入搜索词,结果显示在文本块中,要求是搜索词匹配应在文本块中突出显示。
我见过一些在文本块中突出显示搜索词的示例,但没有一个使用 mvvm 模式。我在视图模型中绑定文本块的文本属性,我尝试访问内联集合以指定不同的标签,但后来发现内联集合不可绑定。
<TextBlock Text="{Binding Description}"/>
此描述正在 ViewModel 中设置
I have a search textbox on silverlight app where user types in search term and results are displayed in textblock, requirement is the search term matches should be highlighted in textblocks.
I have seen some examples of highlighting search terms in textblock but none using mvvm pattern. Im binding text property of textblock in viewmodel, i tried to access inline collection to specify different tags but later came to know that inline collection is not bindable.
<TextBlock Text="{Binding Description}"/>
This description is being set in ViewModel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Will 提到的,这是 View 功能,因此将其放在 Views 代码后面是可以接受的。
在你的 .xaml.cs 中
As Will mentioned, this is View functionality, so putting it in the Views code behind is acceptable.
in your .xaml.cs
好吧,我知道如何在 MVVM 中做到这一点。
我使用 Telerik 的
RadRichTextBox
控件来完成该任务。我创建了一个新类,其依赖属性为
bindableXaml
&将字符串转换为 XAML 的方法(在代码中创建新的RadRichTextBox
来突出显示此工作,并将结果返回为radDocument
)RadRichTextBox
,因此,在 ViewModel 中,来自 DB 的字符串通过 StringToXaml 方法转换为所需的 XAML 格式。您可以使用RichTextBox
实现相同的结果,但其中创建的 XAML 将需要StringBuilder
和RegEx.Match()
来执行突出显示工作。OK i figured out how to do it in MVVM.
I used a
RadRichTextBox
control from Telerik to achieve the task.I created a new class with dependency property as
bindableXaml
& method which converts string to XAML(created newRadRichTextBox
in code to do highlight work in this and returned result asradDocument
)RadRichTextBox
, so inViewModel
the string from DB is converted into desired XAML format viaStringToXaml
method. You can achieve the same result usingRichTextBox
but the XAML created in it will requireStringBuilder
andRegEx.Match()
to do highlight work.