If I globally enable spell checking in App.xaml...
<Application.Resources>
<Style TargetType="TextBox">
<Setter Property="SpellCheck.IsEnabled"
Value="True" />
</Style>
</Application.Resources>
...then I get red underlines and spell checking in all textboxes in the application, irrespective of where they are.
If I want to add a custom dictionary, then I have to use code similar to the one shown in this SO answer, and then call it as follows...
public MainWindow() {
InitializeComponent();
Loaded += (_, __) => Helpers.SetCustomDictionary(this);
}
(code for helper method shown lower down)
This works fine for textboxes that are shown when the window first loads, but if I have a tab control, and the default tab has a textbox, then the custom dictionary is not applied.
I tried calling Helpers.SetCustomDictionary(this)
when the tab loaded, but that didn't work either. I can see that the method is called when the window loads, and my guess is that at that stage, the tab's contents haven't been created, so the method doesn't find them to set the custom dictionary.
The only thing I found that worked was calling it when the individual textbox itself was loaded. However, this is painful, as I have to do this for every single textbox individually.
Anyone know of a way to get the custom dictionary working for textboxes that are not visible when the window first loads?
Thanks
P.S. Here is the code for the helper method, which uses the FindAllChildren()
method shown in the linked SO reply...
public static void SetCustomDictionary(DependencyObject parent) {
Uri uri = new Uri("pack://application:,,,/CustomDictionary.lex");
List<TextBox> textBoxes = new List<TextBox>();
FindAllChildren(parent, ref textBoxes);
foreach (TextBox tb in textBoxes) {
if (tb.SpellCheck.IsEnabled && !tb.SpellCheck.CustomDictionaries.Contains(uri)) {
tb.SpellCheck.CustomDictionaries.Add(uri);
}
}
}