#949 – Add a Custom Dictionary for Spell Checking in a TextBox
November 13, 2013 2 Comments
The built-in spell checker in a TextBox uses a predefined dictionary to look up words. This means that there may be words flagged as misspelled because they are not in the dictionary.
You can add a custom dictionary to WPF’s spell checker by creating a lexicon (.lex) file. To create a custom dictionary, start by creating a .lex file in your project.
Add any custom words to your dictionary, one line at a time.
Set the Build Action of the .lex file to Resource.
In the .xaml file containing the TextBox element, define a namespace that refers to the system assembly.
xmlns:sys="clr-namespace:System;assembly=system"
And set the SpellCheck.CustomDictionaries element using a Uri to refer to your dictionary.
<TextBox Name="txtMyText" Margin="5" Height="100" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" SpellCheck.IsEnabled="True"> <SpellCheck.CustomDictionaries> <sys:Uri>pack://application:,,,/MyDictionary.lex</sys:Uri> </SpellCheck.CustomDictionaries> </TextBox>
The spell checker now recognizes your custom words.