Custom Dictionary in WPF SpellChecker

18 Nov 201824 minutes to read

You can use a default and custom dictionaries to spell check the document based on your need. You can also spell check for any culture and languages using various dictionaries.

Default SpellCheck Dictionary

WPF SpellChecker provides a built-in dictionary for the English language and returns suitable suggestions for misspelled words.

Load your own dictionaries for any language

You can add your own dictionary to the SfSpellChecker.Dictionaries collection. WPF SpellChecker supports three standard dictionary file formats:

  1. Hunspell
  2. Ispell
  3. OpenOffice

The built-in dictionary is disabled when a custom dictionary is added to WPF SpellChecker.

SpellCheck using Hunspell dictionary

You can check spelling mistakes using the Hunspell dictionary format. This format contains the following files:

  • Affix file with grammar rules - *.aff
  • Basic-words file - *.dic

Adding Hunspell Dictionary

  1. Add the HunspellDictionary’s required-culture *.aff and *.dic files to the project, and include them as Resource items. In Visual Studio, select the files in Solution Explorer, open the Properties window, and set Build Action to Resource.

Adding Basic word and Grammar files as resource into the application

  1. Create a HunspellDictionary instance and add the basic word & grammar file path to the HunspellDictionary.DictionaryUri & HunspellDictionary.GrammarUri properties and add the culture to the HunspellDictionary.Culture property.

  2. Add the HunspellDictionary into the SfSpellChecker.Dictionaries collection

  3. Setting the required culture to the SfSpellChecker.Culture property.

The following code snippets show how to add a Hunspell dictionary to the SpellChecker. Please refer to Adding WPF SfSpellChecker to an application to learn how to configure WPF SpellChecker.

<Grid>
       <StackPanel>
           <TextBox 
               Text="Nous sommevs heureusre de vous avochir ici"
               Name="textbox"
               TextWrapping="Wrap">
   
               <!--Adding Spellchecker to the TextBox-->
               <syncfusion:SfSpellChecker.SpellChecker>
                   <syncfusion:SfSpellChecker
                       Culture="fr-FR"
                       x:Name="spellChecker"
                       EnableSpellCheck="True">
                       <syncfusion:SfSpellChecker.Dictionaries>
   
                           <!--Adding French cultured Hunspell dictionary-->
                           <syncfusion:HunspellDictionary DictionaryUri="/HunSpellCheck;component/French/fr-FR.dic"
                                                          GrammarUri="/HunSpellCheck;component/French/fr-FR.aff"
                                                          Culture="fr-FR"/>
                       </syncfusion:SfSpellChecker.Dictionaries>
                   </syncfusion:SfSpellChecker>
               </syncfusion:SfSpellChecker.SpellChecker>
           </TextBox>
           <Button 
               Content="Spell Check"
               Click="SpellCheck_ButtonClick"                
               HorizontalAlignment="Center"></Button>
       </StackPanel>
   </Grid>
//Creating a culture instance
   CultureInfo culture = new CultureInfo("fr-FR");
   
   SfSpellChecker spellChecker = new SfSpellChecker();
   
   // Adding Hunspell dictonaries in Dictionaries collection
   spellChecker.Dictionaries = new DictionaryCollection();
   
   //Add French culture Hunspell dictionary
   spellChecker.Dictionaries.Add(
       new HunspellDictionary()
       {
           Culture = culture,
           GrammarUri = new Uri("/HunSpellCheck;component/French/fr-FR.aff", UriKind.Relative),
           DictionaryUri = new Uri("/HunSpellCheck;component/French/fr-FR.dic", UriKind.Relative)
       }
   );
   
   //Setting a French culture for SpellChecker
   spellChecker.Culture = culture;
   
   //Assigning a spellchecker to the TextBox
   SfSpellChecker.SetSpellChecker(textbox, spellChecker);
//Call SpellCheck method to open SpellCheck on button click
   private void SpellCheck_ButtonClick(object sender, RoutedEventArgs e) {
       spellChecker.PerformSpellCheckUsingDialog();
   }

You can add multiple HunspellDictionary with various culture files into the SfSpellChecker.Dictionaries collection. Based on the SfSpellChecker.Culture respective HunspellDictionary is used for spell check.

SpellCheck using Hunspell dictionary

SpellCheck using Ispell dictionary

You can check spelling mistakes using the Ispell dictionary format. The Ispell dictionary contains the following files:

  • Affix file with grammar rules - *.aff
  • Basic-words file - *.xlg or *.dic

Adding Ispell Dictionary

  1. Add the IspellDictionary’s required-culture *.aff and *.dic files to the project, and include them as Resource items.

Adding Basic word and Grammar files as resource into the application

  1. Create a IspellDictionary instance and add the basic word & grammar file path to the IspellDictionary.DictionaryUri & IspellDictionary.GrammarUri properties and add the culture to the IspellDictionary.Culture property.

  2. Add the IspellDictionary into the SfSpellChecker.Dictionaries collection

  3. Setting the required culture to the SfSpellChecker.Culture property.

    <Grid>
        <StackPanel>
            <TextBox 
                Text="gracsq por venizr por favor ven de nuevoq"
                Name="textbox"
                TextWrapping="Wrap">
       
                <!--Adding Spellchecker to the TextBox-->
                <syncfusion:SfSpellChecker.SpellChecker>
                    <syncfusion:SfSpellChecker
                        Culture="es-ES"
                        x:Name="spellChecker"
                        EnableSpellCheck="True">
                        <syncfusion:SfSpellChecker.Dictionaries>
       
                            <!--Adding Spanish cultured Ispell dictionary-->
                            <syncfusion:IspellDictionary DictionaryUri="/IspellCheck;component/Spanish/es-ES.dic"
                                                         GrammarUri="/IspellCheck;component/Spanish/es-ES.aff"
                                                         Culture="es-ES"/>
                        </syncfusion:SfSpellChecker.Dictionaries>
                    </syncfusion:SfSpellChecker>
                </syncfusion:SfSpellChecker.SpellChecker>
            </TextBox>
            <Button 
                Content="Spell Check"
                Click="SpellCheck_ButtonClick"                
                HorizontalAlignment="Center"></Button>
        </StackPanel>
    </Grid>
    //Creating a culture instance
    CultureInfo culture = new CultureInfo("es-ES");
       
    SfSpellChecker spellChecker = new SfSpellChecker();
       
    // Adding Ispell dictonaries in Dictionaries collection
    spellChecker.Dictionaries = new DictionaryCollection();
       
    //Add Spanish culture Ispell dictionary
    spellChecker.Dictionaries.Add(
        new IspellDictionary()
        {
            Culture = culture,
            GrammarUri = new Uri("/IspellCheck;component/Spanish/es-ES.aff", UriKind.Relative),
            DictionaryUri = new Uri("/IspellCheck;component/Spanish/es-ES.dic", UriKind.Relative)
        }
    );
       
    //Setting a Spanish culture for SpellChecker
    spellChecker.Culture = culture;
       
    //Assigning a spellchecker to the TextBox
    SfSpellChecker.SetSpellChecker(textbox, spellChecker);
    <div class="tabs">
       
    <ul class="nav nav-tabs" role="tablist">
       
    <li role="presentation" class=""><a data-target="#5wup7v4u4falflyn984ij1tvfmzomjgz-csharp" aria-controls="home" role="tab" data-toggle="tab" data-tab-name="C#" data-original-lang="csharp">C#</a></li>
       
    </ul>
       
    <div class="tab-content">
       
       
    <div role="tabpanel" class="tab-pane" id="5wup7v4u4falflyn984ij1tvfmzomjgz-csharp" data-original-lang = "csharp" ><div class="highlight"><pre><code class="prettyprint language-csharp" data-lang="csharp"><span></span><span class="c1">//Call SpellCheck method to open SpellCheck on button click</span>
    <span class="k">private</span> <span class="k">void</span> <span class="nf">SpellCheck_ButtonClick</span><span class="p">(</span><span class="kt">object</span> <span class="n">sender</span><span class="p">,</span> <span class="n">RoutedEventArgs</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">spellChecker</span><span class="p">.</span><span class="n">PerformSpellCheckUsingDialog</span><span class="p">();</span>
    <span class="p">}</span></code></pre></div>
    </div>
       
    </div>
       
    </div>
    

You can add multiple IspellDictionary with various culture files into the SfSpellChecker.Dictionaries collection. Based on the SfSpellChecker.Culture respective IspellDictionary is used for spell check.

SpellCheck using Ispell dictionary

SpellCheck using OpenOffice dictionary

You can check spelling mistakes using the OpenOffice dictionary format. The OpenOffice dictionary contains the following files:

  • Affix file with grammar rules - *.aff
  • Basic-words file - *.dic

Adding OpenOffice Dictionary

  1. Add the OpenOfficeDictionary’s required-culture *.aff and *.dic files to the project, and include them as Resource items.

Adding Basic word and Grammar files as resource into the application

  1. Create a OpenOfficeDictionary instance and add the basic word & grammar file path to the OpenOfficeDictionary.DictionaryUri & OpenOfficeDictionary.GrammarUri properties and add the culture to the OpenOfficeDictionary.Culture property.

  2. Add the OpenOfficeDictionary into the SfSpellChecker.Dictionaries collection

  3. Setting the required culture to the SfSpellChecker.Culture property.

    <Grid>
        <StackPanel>
            <TextBox 
                Text="gracsq por venizr por favor ven de nuevoq"
                Name="textbox"
                TextWrapping="Wrap">
       
                <!--Adding Spellchecker to the TextBox-->
                <syncfusion:SfSpellChecker.SpellChecker>
                    <syncfusion:SfSpellChecker
                        Culture="es-ES"
                        x:Name="spellChecker"
                        EnableSpellCheck="True">
                        <syncfusion:SfSpellChecker.Dictionaries>
       
                            <!--Adding Spanish cultured OpenOffice dictionary-->
                            <syncfusion:OpenOfficeDictionary DictionaryUri="/OpenOfficeSpellCheck;component/Spanish/es-ES.dic"
                                                             GrammarUri="/OpenOfficeSpellCheck;component/Spanish/es-ES.aff"
                                                             Culture="es-ES"/>
                        </syncfusion:SfSpellChecker.Dictionaries>
                    </syncfusion:SfSpellChecker>
                </syncfusion:SfSpellChecker.SpellChecker>
            </TextBox>
            <Button 
                Content="Spell Check"
                Click="SpellCheck_ButtonClick"                
                HorizontalAlignment="Center"></Button>
        </StackPanel>
    </Grid>
    //Creating a Spanish culture instance
    CultureInfo culture = new CultureInfo("es-ES");
       
    SfSpellChecker spellChecker = new SfSpellChecker();
       
    // Adding OpenOffice dictonaries in Dictionaries collection
    spellChecker.Dictionaries = new DictionaryCollection();
       
    //Add Spanish culture OpenOffice dictionary
    spellChecker.Dictionaries.Add(
        new OpenOfficeDictionary()
        {
            Culture = culture,
            GrammarUri = new Uri("/OpenOfficeSpellCheck;component/Spanish/es-ES.aff", UriKind.Relative),
            DictionaryUri = new Uri("/OpenOfficeSpellCheck;component/Spanish/es-ES.dic", UriKind.Relative)
        }
    );
       
    //Setting a Spanish culture for SpellChecker
    spellChecker.Culture = culture;
       
    //Assigning a spellchecker to the TextBox
    SfSpellChecker.SetSpellChecker(textbox, spellChecker);
    <div class="tabs">
       
    <ul class="nav nav-tabs" role="tablist">
       
    <li role="presentation" class=""><a data-target="#m2l40ivzggdj6a6uw9orsuoygjs3z8le-csharp" aria-controls="home" role="tab" data-toggle="tab" data-tab-name="C#" data-original-lang="csharp">C#</a></li>
       
    </ul>
       
    <div class="tab-content">
       
       
    <div role="tabpanel" class="tab-pane" id="m2l40ivzggdj6a6uw9orsuoygjs3z8le-csharp" data-original-lang = "csharp" ><div class="highlight"><pre><code class="prettyprint language-csharp" data-lang="csharp"><span></span><span class="c1">//Call SpellCheck method to open SpellCheck on button click</span>
    <span class="k">private</span> <span class="k">void</span> <span class="nf">SpellCheck_ButtonClick</span><span class="p">(</span><span class="kt">object</span> <span class="n">sender</span><span class="p">,</span> <span class="n">RoutedEventArgs</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">spellChecker</span><span class="p">.</span><span class="n">PerformSpellCheckUsingDialog</span><span class="p">();</span>
    <span class="p">}</span></code></pre></div>
    </div>
       
    </div>
       
    </div>
    

You can add multiple OpenOfficeDictionary with various culture files into the SfSpellChecker.Dictionaries collection. Based on the SfSpellChecker.Culture respective OpenOfficeDictionary is used for spell check.

SpellCheck using OpenOffice dictionary

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.

You can add multiple CustomDictionary for each culture using SfSpellChecker.Dictionaries collection. If you load the WPF SpellChecker with en-US culture, then you can add your custom words only on en-US cultured CustomDictionary.

Adding Custom Dictionary

  1. Create a custom dictionary text file, set build action as None and set Copy to Output Directory to Copy if newer.

Adding custom word files as resource into the application

  1. Create a CustomDictionary instance and add the custom word file path to the CustomDictionary.DictionaryUri property and add the culture to the CustomDictionary.Culture property.

  2. Add the CustomDictionary into the SfSpellChecker.Dictionaries collection

  3. Setting the required culture to the SfSpellChecker.Culture property.

    <Grid>
        <StackPanel>
            <TextBox 
                Text="Ribbn illustrats the Microsoft illustrats Offce 2007 UI. Our prduct exposes most of the featres of the new UI and eeps intact winhth the Slverlight architecture. Configuring and designing the layout is very easy through XAML code. Ribbon tabs and Ribbon bars are the main client area in Ribbon. Ribbon tabs will allow your end users to navigate and find the appropriate tools for the task at hand. The Ribbon bars will contain the Ribbon tools."
                Name="textbox"
                TextWrapping="Wrap">
       
                <!--Adding Spellchecker to the TextBox-->
                <syncfusion:SfSpellChecker.SpellChecker>
                    <syncfusion:SfSpellChecker
                        Culture="en-US"
                        x:Name="spellChecker"
                        EnableSpellCheck="True">
                        <syncfusion:SfSpellChecker.Dictionaries>
       
                            <!--Adding english cultured custom dictionary-->
                            <syncfusion:CustomDictionary DictionaryUri="E:/SpellcheckerDemo/bin/Debug/English/Custom_en-US.txt"
                                                         Culture="en-US"/>
       
                            <!--Adding english cultured OpenOffice dictionary-->
                            <syncfusion:OpenOfficeDictionary DictionaryUri="/CustomSpellCheck;component/US/en-US.dic"
                                                             GrammarUri="/CustomSpellCheck;component/US/en-US.aff"
                                                             Culture="en-US"/>
                               
                        </syncfusion:SfSpellChecker.Dictionaries>
                    </syncfusion:SfSpellChecker>
                </syncfusion:SfSpellChecker.SpellChecker>
            </TextBox>
            <Button 
                Content="Spell Check"
                Click="SpellCheck_ButtonClick"                
                HorizontalAlignment="Center"></Button>
        </StackPanel>
    </Grid>
    //Creating a culture instance
    CultureInfo culture = new CultureInfo("en-US");
       
    SfSpellChecker spellChecker = new SfSpellChecker();
       
    // Get the current PROJECT directory
    Uri CustomDict_uri= new Uri(Directory.GetCurrentDirectory()+  
       
                                @"\English\Custom_en-US.txt", UriKind.Absolute); 
       
    //Add Custom dictionary for US culture
    spellChecker.Dictionaries.Add(
        new CustomDictionary()
        {
            Culture = culture,
            DictionaryUri = CustomDict_uri
        }
    );
       
    //Add US culture OpenOffice dictionary
    spellChecker.Dictionaries.Add(
        new OpenOfficeDictionary()
        {
            Culture = culture,
            GrammarUri = new Uri("/CustomSpellCheck;component/US/en-US.aff", UriKind.Relative),
            DictionaryUri = new Uri("/CustomSpellCheck;component/US/en-US.dic", UriKind.Relative)
        }
       
    //Setting a US culture for SpellChecker
    spellChecker.Culture = culture;
       
    //Assigning a spellchecker to the TextBox
    SfSpellChecker.SetSpellChecker(textbox, spellChecker);
    <div class="tabs">
       
    <ul class="nav nav-tabs" role="tablist">
       
    <li role="presentation" class=""><a data-target="#g6o53h5ix4gj3f17to9044jna0gkn1wg-csharp" aria-controls="home" role="tab" data-toggle="tab" data-tab-name="C#" data-original-lang="csharp">C#</a></li>
       
    </ul>
       
    <div class="tab-content">
       
       
    <div role="tabpanel" class="tab-pane" id="g6o53h5ix4gj3f17to9044jna0gkn1wg-csharp" data-original-lang = "csharp" ><div class="highlight"><pre><code class="prettyprint language-csharp" data-lang="csharp"><span></span><span class="c1">//Call SpellCheck method to open SpellCheck on button click</span>
    <span class="k">private</span> <span class="k">void</span> <span class="nf">SpellCheck_ButtonClick</span><span class="p">(</span><span class="kt">object</span> <span class="n">sender</span><span class="p">,</span> <span class="n">RoutedEventArgs</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">spellChecker</span><span class="p">.</span><span class="n">PerformSpellCheckUsingDialog</span><span class="p">();</span>
    <span class="p">}</span></code></pre></div>
    </div>
       
    </div>
       
    </div>
    

As custom dictionary hold extra words that is not available in standard dictionary, it is always used in conjunction with standard dictionary.

If you use the custom dictionary without standard dictionary, all words that are not included in the dictionary will be shown as error words.

You can add multiple CustomDictionary with various culture word files into the SfSpellChecker.Dictionaries collection. Based on the SfSpellChecker.Culture respective CustomDictionary is used for spell check.

SpellCheck using Custom dictionary

View Sample in GitHub

Switch language (culture) at runtime

You can add Hunspell, Ispell, or OpenOffice dictionaries one or more times with various cultures into the SfSpellChecker.Dictionaries collection. The dictionary that matches the current SfSpellChecker.Culture is used for spell checking. You can change the spell-check culture at runtime by changing the SfSpellChecker.Culture property.

<Grid>
    <StackPanel>
        <TextBox
            Text="Nous sommevs heureusre de vous avochir ici"
            Name="textbox"
            TextWrapping="Wrap">
            <!--Adding SpellChecker to the TextBox-->
            <syncfusion:SfSpellChecker.SpellChecker>
                <syncfusion:SfSpellChecker
                    Culture="fr-FR"
                    x:Name="spellChecker"
                    EnableSpellCheck="True">
                    <syncfusion:SfSpellChecker.Dictionaries>

                        <!--Adding French cultured Hunspell dictionary-->
                        <syncfusion:HunspellDictionary DictionaryUri="/HunSpellCheck;component/French/fr-FR.dic"
                                                       GrammarUri="/HunSpellCheck;component/French/fr-FR.aff"
                                                       Culture="fr-FR"/>

                        <!--Adding Spanish cultured Hunspell dictionary-->
                        <syncfusion:HunspellDictionary DictionaryUri="/HunSpellCheck;component/Spanish/es-ES.dic"
                                                         GrammarUri="/HunSpellCheck;component/Spanish/es-ES.aff"
                                                         Culture="es-ES"/>

                        <!--Adding English cultured Hunspell dictionary-->
                        <syncfusion:HunspellDictionary DictionaryUri="/HunSpellCheck;component/US/en-US.dic"
                                                         GrammarUri="/HunSpellCheck;component/US/en-US.aff"
                                                         Culture="en-US"/>

                    </syncfusion:SfSpellChecker.Dictionaries>
                </syncfusion:SfSpellChecker>
            </syncfusion:SfSpellChecker.SpellChecker>
        </TextBox>
        <Button
            Content="Spell Check"
            Click="SpellCheck_ButtonClick"
            HorizontalAlignment="Center"/>
    </StackPanel>
</Grid>
// Required usings:
// using System;
// using System.Globalization;
// using Syncfusion.Windows.Controls;

SfSpellChecker spellChecker = new SfSpellChecker();

// Adding Hunspell dictionaries in Dictionaries collection
spellChecker.Dictionaries = new DictionaryCollection();

//Add French culture Hunspell dictionary
spellChecker.Dictionaries.Add(
    new HunspellDictionary()
    {
        Culture = new CultureInfo("fr-FR"),
        GrammarUri = new Uri("/HunSpellCheck;component/French/fr-FR.aff", UriKind.Relative),
        DictionaryUri = new Uri("/HunSpellCheck;component/French/fr-FR.dic", UriKind.Relative)
    });

//Add Spanish culture Hunspell dictionary
spellChecker.Dictionaries.Add(
    new HunspellDictionary()
    {
        Culture = new CultureInfo("es-ES"),
        GrammarUri = new Uri("/HunSpellCheck;component/Spanish/es-ES.aff", UriKind.Relative),
        DictionaryUri = new Uri("/HunSpellCheck;component/Spanish/es-ES.dic", UriKind.Relative)
    });

//Add US culture Hunspell dictionary
spellChecker.Dictionaries.Add(
    new HunspellDictionary()
    {
        Culture = new CultureInfo("en-US"),
        GrammarUri = new Uri("/HunSpellCheck;component/US/en-US.aff", UriKind.Relative),
        DictionaryUri = new Uri("/HunSpellCheck;component/US/en-US.dic", UriKind.Relative)
    });

//Set the initial culture for the SpellChecker
spellChecker.Culture = spellChecker.Dictionaries[0].Culture;

//Assigning a spellchecker to the TextBox
SfSpellChecker.SetSpellChecker(textbox, spellChecker);
//Call SpellCheck method to open SpellCheck on button click
private void SpellCheck_ButtonClick(object sender, RoutedEventArgs e) {
    spellChecker.PerformSpellCheckUsingDialog();
}

SpellCheck using Hunspell dictionary

Here, SpellChecker.Culture is initially fr-FR, so the fr-FR Hunspell dictionary is used as the spell-check dictionary. After switching the Culture to es-ES, the Spanish dictionary is used.