4

This is a similar question to this one, but I'm hoping that a better answer has come along in the six years since it was asked.

I have a custom dictionary that I want to use in all textboxes in the application. I don't seem to be able to set the SpellCheck.CustomDictionaries property in a style, like I can with the SpellCheck.IsEnabled property, and I don't want to have to add the setter to every textbox individually.

The answer posted in that question requires hooking in to the Loaded event of every window where you want to use the custom dictionary. We have a large application that is growing all the time, and do not want to have to add handlers for every window, as well as rely on developers remembering to add the code when they add a new window.

Is there any better way to specify a custom dictionary for the whole application? Thanks.

DreamingOfSleep
  • 1,208
  • 1
  • 11
  • 23

1 Answers1

1

Probably not what you wanted, but I used this EventManager.RegisterClassHandler to handle all of certain type's certain event.

For ex, here I am registering to all TextBoxes' LoadedEvent in MainWindow's constructor:

EventManager.RegisterClassHandler(typeof(TextBox), FrameworkElement.LoadedEvent, new RoutedEventHandler(SetCustomDictionary), true);

and define the RoutedEventHandler:

private void SetCustomDictionary(object sender, RoutedEventArgs e)
{
    var textBox = e.OriginalSource as TextBox;
    if (textBox != null)
    {
        textBox.SpellCheck.IsEnabled = true;

        Uri uri = new Uri("pack://application:,,,/MyCustom.lex");
        if (!textBox.SpellCheck.CustomDictionaries.Contains(uri))
            textBox.SpellCheck.CustomDictionaries.Add(uri);
    }
}
kurakura88
  • 2,185
  • 2
  • 12
  • 18
  • Thanks for the reply, but I couldn't get this code to work. I didn't get any errors, but the textboxes all showed the words in my custom dictionary as typos. Also, am I right in thinking that I'd need to do this in every window in the app? If so, it's basically the same as the code in the post I linked. I was hoping there was some way of setting this once, and have it apply over the whole application. Please explain if I got it wrong. Thanks again. – DreamingOfSleep May 08 '18 at 13:34
  • I haven't tried, but may be if you put it on the app level once, you could apply to all of the windows. I did look the code you posted and just reapplied with the `EventManager.RegisterClassHandler` method (hoping to make it work with less writing). – kurakura88 May 09 '18 at 01:33
  • As I said, I couldn't actually get the code to work at all, so I don't know if it would work at the app level – DreamingOfSleep May 09 '18 at 14:49