3
votes

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.

1

1 Answers

1
votes

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