简体   繁体   English

如何在WPF RichTextBox中使工作自定义词典

[英]how to make working custom dictionary in wpf richtextbox

I've tried to add custom dictionary (*.lex format, utf-16 encode) in RichTextBox to make spellcheck, but it doesn't works. 我试图在RichTextBox中添加自定义词典(* .lex格式,utf-16编码)以进行拼写检查,但是它不起作用。 If i using such code for TextBox, it works. 如果我为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; 
}

I've already added #LID1049 in dictionary, but it has no effect. 我已经在字典中添加了#LID1049,但没有效果。 Do you know how to fix this? 你知道如何解决这个问题吗?

There are two ways adding custom dictionaries 有两种添加自定义词典的方式

The first custom dictionary (customwords.lex) is added in XAML XAML中添加了第一个自定义词典(customwords.lex)

<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>

and the Second The second custom dictionary (customwords2.lex) is added in the event handler, The file is included as a resource file and compiled into the application assembly that is named WPFCustomDictionary 第二个第二个自定义词典(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"));
}

Does this works for you??? 这对您有用吗???

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM