navigation
 Tuesday, September 23, 2008

While working on some custom editing controls in a WPF application, I began wishing I could provide my users with some simple spell-checking features.  Although I hadn't used it, I was sure that the RichTextBox control would allow spellchecking, but I was doubtful that my simple TextBoxes would be able to do the same thing.  I was happy to be proven very wrong! 

WPF TextBoxes do allow for spellchecking features, and enabling the feature is as easy as setting a boolean.  Here's the style XAML for two controls which enable spellchecking.

<Style TargetType="{x:Type TextBox}">
    <Setter Property="SpellCheck.IsEnabled" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Background="{TemplateBinding Background}" x:Name="Bd" 
                BorderBrush="{TemplateBinding BorderBrush}" 
                BorderThickness="{TemplateBinding BorderThickness}"
                CornerRadius="5">
                    <ScrollViewer x:Name="PART_ContentHost"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="{x:Type RichTextBox}">
    <Setter Property="SpellCheck.IsEnabled" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RichTextBox}">
                <Border Background="{TemplateBinding Background}" x:Name="Bd" 
                BorderBrush="{TemplateBinding BorderBrush}" 
                BorderThickness="{TemplateBinding BorderThickness}"
                CornerRadius="5">
                    <ScrollViewer x:Name="PART_ContentHost"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Now, when I add some text controls to my window, misspelled words will be highlighted immediately, and a context menu will provide spelling suggestions.  From a user's standpoint, this is familiar territory, because it acts just like what we find in Outlook or Word.

image

Now, it is my understanding that the spellchecker uses the dictionary from Office, and that using different language dictionaries is still a bit of a problem, but the simplicity here is definitely a step in the right direction.  You'll also want to be sure that you don't use any of your own custom context menus which may override the spellchecker's menu.  To learn more about spellchecking on the TextBox control, check out this link from MSDN.

 | 
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, i, strike, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview