Skip to main content

How to: Check Spelling in a WPF Grid Cell

  • 3 minutes to read

The following example demonstrates how to check the spelling of a GridControl cell’s content.

When the editor is activated, the spelling form appears. If the SpellingSettings.CheckAsYouType property is set to true, the misspelled words are highlighted.

DXSpellChecker_GridCell

Create a Custom Behavior Implementation

In the code-behind, create a DXSpellChecker descendant. Implement a property that returns the Grid object and override the class’s OnAttached and OnDetaching methods.

using DevExpress.Xpf.Editors;
using DevExpress.Xpf.Grid;
using DevExpress.Xpf.SpellChecker;
using System;
using System.Windows.Threading;

public class GridControlSpellChecker : DXSpellCheckerBase<GridControl>
{
    GridControl Grid { get { return AssociatedObject; } }
    protected override void OnAttached() {
        base.OnAttached();
        Grid.Dispatcher.BeginInvoke(new Action(() =>
        SubscribeToEvents()), DispatcherPriority.Loaded);
    }

    protected override void OnDetaching() {
            UnsubscribeFromEvents();
            base.OnDetaching();
    }
//...
}

Subscribe to Events

In the created class, handle the current GridControl view‘s ShownEditor event. This event is raised when the grid cell is activated. Retrieve the active editor and run the spell checker as shown below:

using DevExpress.XtraSpellChecker;
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.Grid;
using DevExpress.Xpf.SpellChecker;

private void CardView_ShownEditor(object sender, EditorEventArgs e)
{
    var cardView = (sender as CardView);
    BaseEdit activeEditor = cardView.ActiveEditor;
    if (SpellChecker.SpellCheckMode == SpellCheckMode.OnDemand)
        CheckActiveEditor(activeEditor);
}
void CheckActiveEditor(BaseEdit activeEditor)
{
    activeEditor.Dispatcher.BeginInvoke(new Action(() =>
    {
        if (SpellChecker.CanCheck(activeEditor))
            SpellChecker.Check(activeEditor);
    }), DispatcherPriority.Loaded);
}

In the SpellChecker.CheckCompleteFormShowing event handler, set the Handled property to true to prevent the The spelling check is complete dialog box from showing when the grid control’s editor does not contain errors.

using DevExpress.Xpf.Editors;
using DevExpress.Xpf.Grid;
using DevExpress.Xpf.SpellChecker;

void Checker_CheckCompleteFormShowing(object sender, FormShowingEventArgs e)
{
    e.Handled = true;
}

Create two methods to subscribe and unsubscribe to ShownEditor and CheckCompleteFormShowing events:

private void SubscribeToEvents() {
    SpellChecker.CheckCompleteFormShowing += Checker_CheckCompleteFormShowing;
    CardView cardView = Grid.View as CardView;
    if (cardView != null)
        cardView.ShownEditor += CardView_ShownEditor;
}
private void UnsubscribeFromEvents() {
    SpellChecker.CheckCompleteFormShowing -= Checker_CheckCompleteFormShowing;
    CardView cardView = Grid.View as CardView;
    if (cardView != null)
        cardView.ShownEditor -= CardView_ShownEditor;
}

Add the Behavior in XAML

In XAML, implement the created behavior for the GridControl. Here you can specify spell checker options and add spelling dictionaries.

<Window xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:dxsc="http://schemas.devexpress.com/winfx/2008/xaml/spellchecker"
xmlns:local="clr-namespace:WpfApplication1">

<dxg:GridControl x:Name="grid"  ItemsSource="{Binding Source}">
    <dxmvvm:Interaction.Behaviors>
        <local:GridControlSpellChecker x:Name="spellChecker" 
                                        Culture="en-US" 
                                        ShowSpellCheckMenu="True">
        </local:GridControlSpellChecker>
    </dxmvvm:Interaction.Behaviors>
    <dxg:GridControl.View>
        <dxg:CardView/>
    </dxg:GridControl.View>
</dxg:GridControl>
</Window>