WPF RichTextBox - 在当前插入符号位置获取整个单词 [英] WPF RichTextBox - get whole word at current caret position

查看:54
本文介绍了WPF RichTextBox - 在当前插入符号位置获取整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 富文本框上启用了拼写,我想在显示带有拼写建议的上下文菜单之前在当前插入符号位置获取拼写错误的单词.

I enabled spelling on my WPF richtextbox and I want to get the misspelled word at current caret position before the context menu with spelling suggestions is displayed.

推荐答案

新方法

    void richTextBox1_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Back)
        {
            TextPointer start = richTextBox1.CaretPosition;
            string text1 = start.GetTextInRun(LogicalDirection.Backward);
            TextPointer end = start.GetNextContextPosition(LogicalDirection.Backward);
            string text2 = end.GetTextInRun(LogicalDirection.Backward);

            richTextBox1.Selection.Select(start, end);
            richTextBox1.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
            richTextBox1.Selection.Select(start, start);
            //e.Handled = true;
        }
    }

这篇关于WPF RichTextBox - 在当前插入符号位置获取整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆