Skip to main content

How to: Bind Dictionaries to the Spell Checker in MVVM Applications

  • 4 minutes to read

This topic demonstrates how to bind dictionaries to the Spell Checker integrated into a supported text-aware control (RichEditControl, TextEdit, MemoEdit, TextBox, or RichTextBox).

Use Spell-Checking Behavior

You can implement spell-checking behavior to integrate the Spell Checker into a text editor. For more information, see Configure Spell-Checking Behavior.

Note

We recommend that you use this approach to enable spell check for supported text-aware controls (RichEditControl, TextEdit, MemoEdit, TextBox, and RichTextBox) and add dictionaries to the Spell Checker.

After you attach spell-checking behavior to a text editor, add dictionaries to the SpellCheckerBase<T>.Dictionaries collection in XAML. The Spell Checker supports the Simple, ISpell, OpenOffice, and Hunspell dictionaries.

The following code sample shows how to add dictionaries to the DXSpellChecker integrated into the TextEdit control:

<Window 
    <!-- ... -->
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
    xmlns:dxspch="http://schemas.devexpress.com/winfx/2008/xaml/spellchecker"
    <!-- ... -->
    <Grid>
        <dxe:TextEdit Text="An ideal dictionarie shouldbe comprised of all the the words in a given langauge">
            <dxmvvm:Interaction.Behaviors>
                <dxspch:DXSpellChecker x:Name="spChecker"
                                       UnderlineColor="#FFAA3A37" 
                                       IgnoreEmails="False" 
                                       Culture="en-US" 
                                       ShowSpellCheckMenu="True"
                                       CheckAsYouType="True" 
                                       SpellingFormType="Word">         
                    <dxspch:DXSpellChecker.Dictionaries>
                        <dxspch:SpellCheckerISpellDictionary AlphabetUri="Dictionaries/EnglishAlphabet.txt" 
                                                             Culture="en-US" 
                                                             DictionaryUri="pack://siteoforigin:,,,/Dictionaries/american.xlg" 
                                                             GrammarUri="pack://siteoforigin:,,,/Dictionaries/english.aff"/>
                    </dxspch:DXSpellChecker.Dictionaries>
                </dxspch:DXSpellChecker>
            </dxmvvm:Interaction.Behaviors>
        </dxe:TextEdit>
    </Grid>
</Window>

Warning

Note that the OpenOffice license cannot be used for commercial projects. Please ensure the relevant license agreement permits use within your application or project.

Use the SpellingSettings Option

You can also use the SpellingSettings option to integrate the Spell Checker into a supported text-aware control and bind dictionaries to the Spell Checker.

In the ViewModel, create a SpellChecker class instance and specify its Culture, SpellingFormType, and SpellCheckMode properties. Create a dictionary collection (DictionarySourceCollection) and add a new dictionary to it. Specify the dictionary file, the affix file, and the culture settings of the dictionary.

View Example

using DevExpress.Xpf.SpellChecker;
using DevExpress.XtraSpellChecker;
// ...
public class MainViewModel
{
    public virtual DictionarySourceCollection Dictionaries { get; set; }
    public virtual SpellChecker SpellChecker { get; set; }

    public MainViewModel()
    {
        // Create a SpellChecker class instance.
        SpellChecker = new SpellChecker();
        // Specify the SpellChecker's properties.
        SpellChecker.SpellingFormType = SpellingFormType.Word;
        SpellChecker.SpellCheckMode = SpellCheckMode.AsYouType;
        SpellChecker.Culture = new CultureInfo("de-DE");
        // Obtain a dictionary collection.
        Dictionaries = GetDictionaries();
    }

    public DictionarySourceCollection GetDictionaries()
    {
        // Create a dictionary collection.
        var collection = new DictionarySourceCollection();
        // Create a Hunspell spell-checking dictionary.
        var dictionary = new HunspellDictionarySource();
        // Specify the dictionary culture settings.
        dictionary.Culture = new CultureInfo("de-DE");
        // Load the dictionary file.
        dictionary.DictionaryUri = new Uri(@"pack://application:,,,/DXSpellCheckerBindingDictionaries;component/Dictionaries/de_DE.dic");
        // Load the affix file.
        dictionary.GrammarUri = new Uri(@"pack://application:,,,/DXSpellCheckerBindingDictionaries;component/Dictionaries/de_DE.aff");
        // Add the dictionary to the collection.
        collection.Add(dictionary);
        return collection;
    }
}

In XAML, bind the created SpellChecker object to the SpellingSettings.SpellChecker property to enable spell check for a text editor. Specify the SpellingSettings attributes applied to the text editor. Bind the dictionary collection to the SpellingSettings.DictionarySourceCollection property to add dictionaries to the Spell Checker.

The code sample below binds dictionaries to the Spell Checker integrated into the TextEdit control:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<!-- ... -->
        xmlns:dxsch="http://schemas.devexpress.com/winfx/2008/xaml/spellchecker"
        <!-- ... -->
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
        <!-- ... -->
        xmlns:dxspch="clr-namespace:DevExpress.Xpf.SpellChecker;assembly=DevExpress.Xpf.SpellChecker.v21.1"
        <!-- ... -->
    <Window.DataContext>
        <ViewModel:MainViewModel/>
    </Window.DataContext>
    <Grid>
    <!-- ... -->
        <dxe:TextEdit x:Name="textEdit1"
                      Text=" Das Regcht hat viele Funktionem. Die wichtigste darunter ist die Sicherung des Friedens im Lande."
                      TextWrapping="Wrap"
                      VerticalContentAlignment="Top"
                      dxsch:SpellingSettings.DictionarySourceCollection="{Binding Dictionaries,UpdateSourceTrigger=PropertyChanged}"/>

        <dxspch:SpellingSettings.SpellChecker>
            <Binding Path="SpellChecker"/>
        </dxspch:SpellingSettings.SpellChecker>
        <dxspch:SpellingSettings.CheckAsYouType>True</dxspch:SpellingSettings.CheckAsYouType>
        <dxspch:SpellingSettings.ShowSpellCheckMenu>True</dxspch:SpellingSettings.ShowSpellCheckMenu>
        <dxspch:SpellingSettings.UnderlineColor>Red</dxspch:SpellingSettings.UnderlineColor>
        <dxspch:SpellingSettings.UnderlineStyle>WavyLine</dxspch:SpellingSettings.UnderlineStyle>
        <!-- ... -->
    </Grid>
    <!-- ... -->
</Window>
See Also