DreamingOfSleep
DreamingOfSleep

Reputation: 1458

Can I set a custom dictionary for all textboxes in my WPF application?

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.

Upvotes: 4

Views: 319

Answers (1)

kurakura88
kurakura88

Reputation: 2305

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

Upvotes: 1

Related Questions