wpf-118949-controls-and-libraries-spell-checker-examples-how-to-check-spelling-of-a-grid-cell.md
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.
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();
}
//...
}
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
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);
}
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.
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.Grid;
using DevExpress.Xpf.SpellChecker;
void Checker_CheckCompleteFormShowing(object sender, FormShowingEventArgs e)
{
e.Handled = true;
}
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:
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;
}
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
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>