Getting Started with WPF SfSpellChecker
18 Nov 201819 minutes to read
This section explains how to create a WPF SpellChecker (SfSpellChecker) and spell check the text.
Control Structure

Assembly deployment
Refer to the control dependencies section to get the list of assemblies or NuGet packages that need to be added as a reference to use the control in any application.
You can find more details about installing the NuGet package in a WPF application in the following link:
Adding WPF SfSpellChecker to an application
Spell-checking operations can be performed on text-editor controls through SfSpellChecker in a WPF application.
You can add the SfSpellChecker to an application by following these steps:
-
Create a WPF project in Visual Studio and include the following assembly:
- Syncfusion.SfSpellChecker.WPF
-
Add a
TextBoxcontrol and set the SfSpellChecker.SpellChecker attached property to perform spell check.
NOTE
The
syncfusion:prefix used below requiresxmlns:syncfusion="http://schemas.syncfusion.com/wpf"to be declared on the root<Window>element.
<Window x:Class="SpellCheckerSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<TextBox
Text="Natusre is an importsant and integral part of mankind. It is one of the greattest blessings for human lifve. Howeverq, nowadays humans fail to recognize it as one. Nature has been an inspiration for numerous poets, writeqrs, artists and more of yesteryears."
Name="textbox"
TextWrapping="Wrap">
<!--Adding SpellChecker to the TextBox-->
<syncfusion:SfSpellChecker.SpellChecker>
<syncfusion:SfSpellChecker
x:Name="spellChecker"
EnableSpellCheck="True"/>
</syncfusion:SfSpellChecker.SpellChecker>
</TextBox>
<Button
Content="Spell Check"
Click="SpellCheck_ButtonClick"
HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
</Window>//Creating a spell checker instance
SfSpellChecker spellChecker = new SfSpellChecker();
//Enabling the spell check (default is true)
spellChecker.EnableSpellCheck = true;
//Assigning a spellchecker to the TextBox
SfSpellChecker.SetSpellChecker(textbox, spellChecker);-
If you want to open the
SfSpellCheckerwhile clicking on theSpell Check button, call the PerformSpellCheckUsingDialog method inside theSpellCheck ButtonClickmethod.//Call SpellCheck method to open SpellCheck on button click private void SpellCheck_ButtonClick(object sender, RoutedEventArgs e) { spellChecker.PerformSpellCheckUsingDialog(); }

NOTE
View Sample in GitHub
Fix spelling mistakes using spell check dialog
-
Open a
SfSpellCheckerby clicking the Spell Check button; it opens as a pop-up containing aTextSpellEditor. -
Error words are highlighted with a red foreground.
-
Replace an error word with a suggested word by double-clicking the suggestion in the list, or by selecting it and pressing the Change button.

NOTE
View Sample in GitHub
Fix spelling mistakes using context menu
You can correct misspelled words by choosing the correct option from the suggestions listed in the context menu. You can get the suggestions by right-clicking the error word. Error words are differentiated by a red underline. You can disable context-menu suggestions by setting the EnableContextMenu property to false. The default value of EnableContextMenu is true.
<Grid>
<StackPanel>
<TextBox
Text="Natusre is an importsant and integral part of mankind. It is one of the greattest blessings for human lifve. Howeverq, nowadays humans fail to recognize it as one. Nature has been an inspiration for numerous poets, writeqrs, artists and more of yesteryears."
Name="textbox"
TextWrapping="Wrap"
VerticalContentAlignment="Top">
<!--Adding SpellChecker to the TextBox-->
<syncfusion:SfSpellChecker.SpellChecker>
<syncfusion:SfSpellChecker
x:Name="spellChecker"
EnableContextMenu="True"
EnableSpellCheck="True"/>
</syncfusion:SfSpellChecker.SpellChecker>
</TextBox>
<Button
Content="Spell Check"
Click="SpellCheck_ButtonClick"
HorizontalAlignment="Center"/>
</StackPanel>
</Grid>//Enable Contextmenu to spellcheck
spellChecker.EnableContextMenu = true;
spellChecker.EnableSpellCheck = true;//Call SpellCheck method to open SpellCheck on button click
private void SpellCheck_ButtonClick(object sender, RoutedEventArgs e) {
spellChecker.PerformSpellCheckUsingDialog();
}
NOTE
View Sample in GitHub
Disable spell checking
If you want to disable the spell check operation, use the EnableSpellCheck property value as false.If the EnableSpellCheck property value is false, you will not be able to use both the context menu and SpellCheck dialogue to perform spell checking operations. The default value of EnableSpellCheck property is true.
<Grid>
<StackPanel>
<TextBox
Text="Natusre is an importsant and integral part of mankind. It is one of the greattest blessings for human lifve. Howeverq, nowadays humans fail to recognize it as one. Nature has been an inspiration for numerous poets, writeqrs, artists and more of yesteryears."
Name="textbox"
TextWrapping="Wrap">
<!--Adding SpellChecker to the TextBox-->
<syncfusion:SfSpellChecker.SpellChecker>
<syncfusion:SfSpellChecker
x:Name="spellChecker"
EnableSpellCheck="False"/>
</syncfusion:SfSpellChecker.SpellChecker>
</TextBox>
<Button
Content="Spell Check"
Click="SpellCheck_ButtonClick"
HorizontalAlignment="Center"/>
</StackPanel>
</Grid>//Restrict the spell check operation
spellChecker.EnableSpellCheck = false;//Call SpellCheck method to open SpellCheck on button click
private void SpellCheck_ButtonClick(object sender, RoutedEventArgs e) {
spellChecker.PerformSpellCheckUsingDialog();
}
NOTE
View Sample in GitHub
Get suggestions for misspelled word
You can get the suggestion list by passing the error word in the below methods.
- GetSuggestions - Returns a list of suggestion words for an error word.
- GetPhoneticWords - Returns a list of phonetic words for an error word.
- GetAnagrams - Returns a list of anagram words for an error word.
//Get a list of suggestion words for an error word
List<string> suggestions = spellChecker.GetSuggestions("Natusre");
//Get a list of phonetic words for an error word
List<string> phoneticWords = spellChecker.GetPhoneticWords("Natusre");
//Get a list of anagram words for an error word
List<string> anagramWords = spellChecker.GetAnagrams("Natusre");Ignore SpellCheck for particular types of text
If you want to ignore the error words such a format like email id’s and link addresses, HTML tags, combination of words and numbers, combination of upper and lower case words, use the respective property value as true from the following table,
| Property | Description | Example |
|---|---|---|
| IgnoreEmailAddress | Specifies whether or not to ignore email address during Spell Check. The Default value is False. | Ex: [email protected] |
| IgnoreHtmlTags | Specifies whether or not to ignore HTML tags during Spell Check. The Default value is False. | Ex: < html></ html> |
| IgnoreUrl | Specifies whether or not to ignore Internet address during Spell Check. The Default value is False. | Ex: https://help.syncfusion.com |
| IgnoreMixedCaseWords | Specifies whether or not to ignore mixed case words during Spell Check. The Default value is False. | Ex: AbCDeFH |
| IgnoreUpperCaseWords | Specifies whether or not to ignore uppercase words during Spell Check. The Default value is False. | Ex: ABCDE > |
| IgnoreAlphaNumericWords | Specifies whether or not to Spell Check numbers or words with numbers during Spell Check. The Default value is False. | Ex: A*&%#9ACe&981 |
<Grid>
<StackPanel>
<TextBox
Text="Natusre is an importsant and integral part of mankind. It is one of the greattest blessings for human lifve. Howeverq, nowadays humans fail to recognize it as one. Nature has been an inspiration for numerous poets, writeqrs, artists and more of yesteryears."
Name="textbox"
TextWrapping="Wrap">
<!--Adding Spellchecker to the TextBox-->
<syncfusion:SfSpellChecker.SpellChecker>
<syncfusion:SfSpellChecker
x:Name="spellChecker"
EnableContextMenu="True"
EnableSpellCheck="True"
IgnoreUrl="True"
IgnoreUpperCaseWords="True"
IgnoreAlphaNumericWords="True"
IgnoreEmailAddress="True"
IgnoreMixedCaseWords="True"
IgnoreHtmlTags="True"/>
</syncfusion:SfSpellChecker.SpellChecker>
</TextBox>
<Button
Content="Spell Check"
Click="SpellCheck_ButtonClick"
HorizontalAlignment="Center"/>
</StackPanel>
</Grid>spellChecker.IgnoreUrl = true;
spellChecker.IgnoreUpperCaseWords = true;
spellChecker.IgnoreAlphaNumericWords = true;
spellChecker.IgnoreEmailAddress = true;
spellChecker.IgnoreMixedCaseWords = true;
spellChecker.IgnoreHtmlTags = true;//Call SpellCheck method to open SpellCheck on button click
private void SpellCheck_ButtonClick(object sender, RoutedEventArgs e) {
spellChecker.PerformSpellCheckUsingDialog();
}SpellCheck for any language(culture)
You can spell check any language(culture) by adding the respective culture to the SfSpellChecker.Culture property and add the dictionaries which contains the basic word file and grammar file to the SfSpellChecker.Dictionaries collection.
The following dictionary types are used for spell-checking,
- Hunspell
- Ispell
- OpenOffice
NOTE
Refer the Load your own dictionaries for any language page to know more about how to add and use the Dictionary for any culture to an application.
Add custom words to dictionary
If you want to add words that is not available in existing dictionary, you can add it using CustomDictionary. This dictionary does not has a grammar file, it accepts only dictionary file that contains a list of words. Users can also add words to this custom dictionary by clicking Add to Dictionary button available in dialog or context menu.
NOTE
Refer the Adding Custom Dictionary page to know more about how to add and use the custom dictionary to an application.
Event to notify when spell check is completed
By default, when the spell check is completed, it will be notified by using the message box that showing the Spell check is completed message. If you want to restrict that message box, you can handle the SpellCheckCompleted event and set the SpellCheckCompletedEventArgs.ShowMessageBox to false.
<Grid>
<StackPanel>
<TextBox
Text="Natusre is an importsant and integral part of mankind. It is one of the greattest blessings for human lifve. Howeverq, nowadays humans fail to recognize it as one. Nature has been an inspiration for numerous poets, writeqrs, artists and more of yesteryears."
Name="textbox"
TextWrapping="Wrap">
<!--Adding SpellChecker to the TextBox-->
<syncfusion:SfSpellChecker.SpellChecker>
<syncfusion:SfSpellChecker
x:Name="spellChecker"
SpellCheckCompleted="SpellChecker_SpellCheckCompleted"
EnableContextMenu="True"
EnableSpellCheck="True"/>
</syncfusion:SfSpellChecker.SpellChecker>
</TextBox>
<Button
Content="Spell Check"
Click="SpellCheck_ButtonClick"
HorizontalAlignment="Center"/>
</StackPanel>
</Grid>spellChecker.SpellCheckCompleted += SpellChecker_SpellCheckCompleted;//Call SpellCheck method to open SpellCheck on button click
private void SpellCheck_ButtonClick(object sender, RoutedEventArgs e) {
spellChecker.PerformSpellCheckUsingDialog();
}
private void SpellChecker_SpellCheckCompleted(object sender, SpellCheckCompletedEventArgs e) {
//Suppress the completion message box
e.ShowMessageBox = false;
}
NOTE
View Sample in GitHub
Theme
WPF SpellChecker (SfSpellChecker) supports various built-in themes. Refer to the below links to apply themes for the SfSpellChecker,
