Search code examples
c#wpfspell-checking

WPF 4.0 SpellCheck issue loading Custom Dictionaries


Has anyone used a custom dictionary into WPF 4.0? I am having an issue getting the Custom Dictionaries to work in my WPF project. I have been trying to follow the example msdn offers but have made no progress.

http://msdn.microsoft.com/en-us/library/system.windows.controls.spellcheck.customdictionaries.aspx

glossary.Definition.SpellCheck.IsEnabled = true;
Uri uri = new Uri(@"pack://application:,,,/Prog.Proj;component/dictionary.lex");
glossary.Definition.SpellCheck.CustomDictionaries.Add(uri);

Due to the nature of my work sub folders have been renamed.

My .lex file is set as a resource file.

EDIT
I am able to get this to work only if I set it up in a separate button event after the page has already loaded. It seems that something is preventing the 'Speller' property of CustomDictionariesSources to load until after a postback? If anyone knows anything on this please post your insight.

FINAL EDIT
My desired text box was within a grid which had a enabled disabled flag that was set deep within the code. Another link commented below talks towards this point. Another issue faced is my page is rendered by parts depending on user selection. To create consistent behavior I am loading my spellcheck as a last step each time my textbox would be loaded/re-loaded.

I created a context menut extension to allow users to either take a suggestion or add to a custom dictionary. I am then submitting my custom dictionary into the registry based on current user. I found this direction to be very user friendly and easy to implement. To retrieve the items back I need to create a temporary file, pack the uri for that file then after loading the custom dictionary I deleted the file.

If this helps you implement your custom spell check or if you have questions please let me know!


Solution

  • The URI in your example is a Disk path to a folder on your C: drive. If you want to access the lex file embedded as a resource within your application, you need to use a "Pack URI."

    Refer to the article which you already linked to, for an example of a Pack URI being used to load a custom dictionary:

    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"));
    }