Back to Devexpress

How to: Check Spelling in a WPF Grid Cell

wpf-118949-controls-and-libraries-spell-checker-examples-how-to-check-spelling-of-a-grid-cell.md

latest6.8 KB
Original Source

How to: Check Spelling in a WPF Grid Cell

  • Jun 12, 2024
  • 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.

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.

csharp
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();
    }
//...
}
vb
Imports DevExpress.Xpf.Editors
Imports DevExpress.Xpf.Grid
Imports DevExpress.Xpf.SpellChecker
Imports System
Imports System.Windows.Threading

Public Class GridControlSpellChecker
    Inherits DXSpellCheckerBase(Of GridControl)

    Private ReadOnly Property Grid() As GridControl
        Get
            Return AssociatedObject
        End Get
    End Property
    Protected Overrides Sub OnAttached()
        MyBase.OnAttached()
        Grid.Dispatcher.BeginInvoke(New Action(Sub() SubscribeToEvents()), DispatcherPriority.Loaded)
    End Sub
'...
End Class

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:

csharp
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);
}
vb
Private Sub CardView_ShownEditor(ByVal sender As Object, ByVal e As EditorEventArgs)
    Dim cardView = (TryCast(sender, CardView))
    Dim activeEditor As BaseEdit = cardView.ActiveEditor
    If SpellChecker.SpellCheckMode = DevExpress.XtraSpellChecker.SpellCheckMode.OnDemand Then
        CheckActiveEditor(activeEditor)
    End If
End Sub
Private Sub CheckActiveEditor(ByVal activeEditor As BaseEdit)
    activeEditor.Dispatcher.BeginInvoke(New Action(Sub()
        If SpellChecker.CanCheck(activeEditor) Then
            SpellChecker.Check(activeEditor)
        End If
    End Sub), DispatcherPriority.Loaded)
End Sub

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.

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

void Checker_CheckCompleteFormShowing(object sender, FormShowingEventArgs e)
{
    e.Handled = true;
}
vb
Private Sub Checker_CheckCompleteFormShowing(ByVal sender As Object, ByVal e As DevExpress.XtraSpellChecker.FormShowingEventArgs)
    e.Handled = True
End Sub

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

csharp
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;
}
vb
Private Sub SubscribeToEvents()
    SpellChecker.CheckCompleteFormShowing += Checker_CheckCompleteFormShowing
    Dim cardView As CardView = TryCast(Grid.View, CardView)
    If cardView IsNot Nothing Then
        cardView.ShownEditor += CardView_ShownEditor
    End If
End Sub
Private Sub UnsubscribeFromEvents()
    SpellChecker.CheckCompleteFormShowing -= Checker_CheckCompleteFormShowing
    Dim cardView As CardView = TryCast(Grid.View, CardView)
    If cardView IsNot Nothing Then
        cardView.ShownEditor -= CardView_ShownEditor
    End If
End Sub

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.

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