Using SpellChecker in TextboxBase

WPF TextBox or RichTextBox or any control inherited from TextboxBase has an inbuilt support for Spellchecking functionality, which you might employ very easily just by enabling the SpellCheck menu for the Textbox. In this article I am going to discuss how you can easily invoke spellchecking functionality in your WPF application.

Basic Usage

WPF provides a new object called SpellCheck. This object can be applied to any TextBoxBase object and thereby the object will behave automatically with the spellcheck functionality. To do this, lets look at the example :
<stackpanel height="150" margin="50">
      <textblock text="Enter Content"></textblock>
      <textbox acceptsreturn="True" spellcheck.isenabled="True" x:name="txtBox">

In the above example, you can see the TextBox txtBox, have SpellChecker enabled. This ensures the textbox automatically detect misspelled words and will put an underline on the word. When you run the application, you will see the textbox to appear like this :
You can see all the misspelled word is shown with red underline. These words can easily be corrected by using the TextBox default ContextMenu. Right clicking on the word will give you a list of suggestions for the current word which you might choose to change the word in the paragraph.

SpellCheck.IsEnabled is actually a Dependency property. So you can easily use it to set in Style setters. You can also use global setter to apply SpellChecking to every control you put in the window very easily.

<style targettype="{x:Type TextBoxBase}" x:key="txtBasic">
<setter Property="SpellCheck.IsEnabled" Value="true" />
<setter Property="SpellCheck.SpellingReform" Value="PreAndPostreform" />

Now you can easily define any TextBox or RichTextBox or any control that derives TextboxBase and use the Style to apply SpellChecker. You can also use

<style basedon="{StaticResource txtBasic}" targettype="{x:Type TextBox}">
<style TargetType="{x:Type RichTextBox}" BasedOn="{StaticResource txtBasic}" />

By doing this, any TextBox or RichTextBox which you place in your Window will automatically have spellChecking enabled.
You can also enable/disable the SpellCheck from codebehind.
SpellCheck.SetIsEnabled(txtBox, true);
SpellCheck.SetSpellingReform(txtBox, SpellingReform.PreAndPostreform);

Spelling error can be detected based on the carat location. TextBoxBase contains a few methods that you can invoke to get Spelling errors. SpellingError is a class which provides the current Spelling error word with an enumerable of Suggessions.

Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice


Post a Comment

Please make sure that the question you ask is somehow related to the post you choose. Otherwise you post your general question in Forum section.

Author's new book

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!