Lesson 3 - Implement Check As You Type Mode

  • 3 minutes to read

This document provides step-by-step instructions on how to implement check-as-you-type functionality in a simple project that uses the SpellChecker component.

TIP

This lesson uses the project created in Lesson 2 - Manage the Spell Checker Programmatically.

Follow the steps below.

Step 1. Bind the SpellChecker defined in Code-Behind

  • Implement a SpellChecker property to provide access to the spell checker component.
  • Set the data context to enable binding.
  • In XAML, add the namespace:
xmlns:dxspch="http://schemas.devexpress.com/winfx/2008/xaml/spellchecker"
  • Bind the SpellingSettings.SpellChecker property to the created SpellChecker property. Note that if the SpellingSettings.SpellChecker property is not specified, an internal spell checker is created. The internal spell checker uses the SpellingSettings specified in XAML.
using System.Windows;
using System.Globalization;
using DevExpress.Xpf.SpellChecker;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        SpellChecker checker;

        public MainWindow()
        {
            DataContext = this;
            checker = new SpellChecker();
            InitializeComponent();
            SpellChecker = checker;
        }

        public SpellChecker SpellChecker
        {
            get { return checker; }
            private set { checker = value; }
        }
    }
}

Step 2. Switch the Check-As-You-Type Mode On

Set the SpellChecker.SpellCheckMode property to SpellCheckMode.AsYouType to enable check-as-you-type mode.

The resulting code is shown below:

public partial class MainWindow : Window
    {
        SpellChecker checker;

        public MainWindow()
        {
            DataContext = this;
            checker = new SpellChecker();
            InitializeComponent();
            checker.Culture = new CultureInfo("pl-PL");
            checker.SpellCheckMode = DevExpress.XtraSpellChecker.SpellCheckMode.AsYouType;
            SpellChecker = checker;
        }

        public SpellChecker SpellChecker
        {
            get { return checker; }
            private set { checker = value; }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // Start checking the text within a text edit control.
            checker.Check(textEdit1);
        }
    }

Step 3. Specify SpellingSettings in XAML

Specify the following values for the SpellingSettings attributes:

<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>
NOTE

If you use the Rich Text Editor, the underline color (red) and appearance (wavy) are predefined and cannot be changed.

You can also use the SpellingSettings.DictionarySourceCollection property to add dictionaries to the spell checker. You can add a dictionary as shown in the following code:

<dxspch:SpellingSettings.DictionarySourceCollection>
    <dxspch:DictionarySourceCollection>
        <dxspch:SpellCheckerOpenOfficeDictionarySource Culture="pl-PL" 
                                                   DictionaryUri="pack://siteoforigin:,,,/Dictionaries/pl_PL.dic" 
                                                   GrammarUri="pack://siteoforigin:,,,/Dictionaries/pl_PL.aff"/>
    </dxspch:DictionarySourceCollection>
</dxspch:SpellingSettings.DictionarySourceCollection>

Step 4. Run the Application

When SpellChecker encounters a word that is not found within available dictionaries or a duplicated word, it marks it with a red wavy line.

CheckAsYouType-Result

Right-click the misspelled word to invoke a menu where you can choose from suggested replacements or select other actions to handle the mistake.