在文本块中突出显示搜索词

发布于 2024-12-17 08:12:15 字数 274 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聽兲甴掵 2024-12-24 08:12:15

正如 Will 提到的,这是 View 功能,因此将其放在 Views 代码后面是可以接受的。

在你的 .xaml.cs 中

private void ButtonSearchRequest_Click(object sender, RoutedEventArgs e)
{
    Search(TextBoxToSearch, TextBoxSearchTerm.Text);
}

private void Search(TextBox tb, string strSearchTerm)
{
    strSearchTerm = strSearchTerm.Trim().ToLower();
    int iNextMatch = tb.Text.ToLower().IndexOf(strSearchTerm);

    if (iNextMatch >= 0)
    {
        tb.Focus();
        tb.CaretIndex = iNextMatch;
        tb.Select(iNextMatch, strSearchTerm.Length);
        tb.ScrollToLine(tb.GetLineIndexFromCharacterIndex(iNextMatch));
    }
 }

As Will mentioned, this is View functionality, so putting it in the Views code behind is acceptable.

in your .xaml.cs

private void ButtonSearchRequest_Click(object sender, RoutedEventArgs e)
{
    Search(TextBoxToSearch, TextBoxSearchTerm.Text);
}

private void Search(TextBox tb, string strSearchTerm)
{
    strSearchTerm = strSearchTerm.Trim().ToLower();
    int iNextMatch = tb.Text.ToLower().IndexOf(strSearchTerm);

    if (iNextMatch >= 0)
    {
        tb.Focus();
        tb.CaretIndex = iNextMatch;
        tb.Select(iNextMatch, strSearchTerm.Length);
        tb.ScrollToLine(tb.GetLineIndexFromCharacterIndex(iNextMatch));
    }
 }
卸妝后依然美 2024-12-24 08:12:15

好吧,我知道如何在 MVVM 中做到这一点。

  1. 我使用 Telerik 的 RadRichTextBox 控件来完成该任务。

  2. 我创建了一个新类,其依赖属性为 bindableXaml &将字符串转换为 XAML 的方法(在代码中创建新的 RadRichTextBox 来突出显示此工作,并将结果返回为 radDocument

  3. ,将其附加到 RadRichTextBox,因此,在 ViewModel 中,来自 DB 的字符串通过 StringToXaml 方法转换为所需的 XAML 格式。您可以使用 RichTextBox 实现相同的结果,但其中创建的 XAML 将需要 StringBuilderRegEx.Match() 来执行突出显示工作。

OK i figured out how to do it in MVVM.

  1. I used a RadRichTextBox control from Telerik to achieve the task.

  2. I created a new class with dependency property as bindableXaml & method which converts string to XAML(created new RadRichTextBox in code to do highlight work in this and returned result as radDocument)

  3. Attached it to RadRichTextBox, so in ViewModel the string from DB is converted into desired XAML format via StringToXaml method. You can achieve the same result using RichTextBox but the XAML created in it will require StringBuilder and RegEx.Match() to do highlight work.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文