如何在WPF richtextbox中制作工作自定义字典

本文关键字:工作 自定义 字典 WPF richtextbox | 更新日期: 2023-09-27 17:51:03

我尝试添加自定义字典(*。lex格式,utf-16编码)在RichTextBox进行拼写检查,但它不工作。如果我对TextBox使用这样的代码,它可以工作。

private void SpellCheckInit()
{   
    // this works            
    txt_Box.SpellCheck.CustomDictionaries.Add(new Uri(@"C:'dictionary.lex"));
    // dictionary language is russian, but this setting makes spellcheck works                  
    txt_Box.Language = System.Windows.Markup.XmlLanguage.GetLanguage("en-GB");
    txt_Box.SpellCheck.IsEnabled = true;
    // this doesn't works
    richtxt.SpellCheck.CustomDictionaries.Add(new Uri(@"C:'dictionary.lex"));
    var ruLang = System.Windows.Markup.XmlLanguage.GetLanguage("ru");
    var enLang = System.Windows.Markup.XmlLanguage.GetLanguage("en-GB");
    richtxt.Language = ruLang;
    // or richtxt.Language = enLang; there are no difference for working
    richtxt.SpellCheck.IsEnabled = true; 
}

我已经在字典中添加了#LID1049,但是没有效果。你知道怎么解决吗?

如何在WPF richtextbox中制作工作自定义字典

有两种方式添加自定义字典

第一个自定义字典(customwords.lex)被添加到XAML

<RichTextBox Margin="38,18,40,0" Name="richTextBox1" Height="45" VerticalAlignment="Top" SpellCheck.IsEnabled="True" >
<SpellCheck.CustomDictionaries>
    <!-- customwords.lex is included as a content file-->
    <sys:Uri>pack://application:,,,/customwords.lex</sys:Uri>
</SpellCheck.CustomDictionaries>

第二个自定义字典(customwords2.lex)被添加到事件处理程序中,该文件作为资源文件包含并编译到名为WPFCustomDictionary 的应用程序程序集中
private void button1_Click(object sender, RoutedEventArgs e)
{
    IList dictionaries = SpellCheck.GetCustomDictionaries(richTextBox1);
    // customwords2.lex is included as a resource file
    dictionaries.Add(new Uri(@"pack://application:,,,/WPFCustomDictionary;component/customwords2.lex"));
}

这个对你有用吗??