TX Text Control .NET for WPF

Integrating TX Spell .NET for WPF

Topic

The WPF TextControl can underline misspelled words, suggest corrections, hyphenate lines and open a thesaurus, but the words themselves come from TX Spell .NET for WPF, a separate product of Text Control. In a WPF window, the spell checker is not a control but a resource of the window, and the editor refers to it by its key. The integration steps below -- packages, licenses, the resource, several editors and deployment -- lead to the TX Spell .NET documentation, which covers every feature in depth.

What Each Product Contributes

TX Text Control .NET for WPFTX Spell .NET for WPF
PackageTXTextControl.TextControl.WPF.SDK 34.0.5TXTextControl.TXSpell.WPF.SDK 11.0.1
In XAMLThe controls in TXTextControl.WPF, assembly TXTextControl.WPFThe TXSpellChecker in TXTextControl.WPF.Proofing, assembly TXSpell.WPF, declared as a resource
At run timeThe red underlines, the context menu of misspelled words, the spelling dialog, the Proofing ribbon tab and hyphenation at the end of a lineThe words: US English dictionary, hyphenation list, synonym list and language recognition, and further languages you add

The members of the TextControl that display the results, such as IsSpellCheckingEnabled and IsHyphenationEnabled, are listed in Spell Checking and Hyphenation.

Install and License Both Products

TX Spell .NET has its own setup, license and release cycle:

  1. Install the setup of TX Spell .NET for WPF, the full or the trial version. It adds its package to Text Control Offline Packages. See Installing the Product.
  2. In your project, choose Project > Manage NuGet Packages..., select Text Control Offline Packages as the package source and install TXTextControl.TXSpell.WPF.SDK.
  3. Build the project. The package embeds its license file, txspell.wpf.license, into your assembly, beside the one of TX Text Control.

TX Spell .NET 11.0 was released together with TX Text Control 33.0 and is the version to use with TX Text Control 34.0. Upgrade both products together; Upgrading from Previous Versions lists what changes.

Connect the Spell Checker in XAML

The following MainWindow.xaml declares a spell checker in the resources of the window and connects it to an editor with a Home and a Proofing tab:

<Window x:Class="MyWPFApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WPF="clr-namespace:TXTextControl.WPF;assembly=TXTextControl.WPF"
        xmlns:spell="clr-namespace:TXTextControl.WPF.Proofing;assembly=TXSpell.WPF"
        Title="MainWindow" Height="600" Width="1000">
    <Window.Resources>
        <spell:TXSpellChecker x:Key="txSpellChecker1" />
    </Window.Resources>
    <DockPanel>
        <Ribbon DockPanel.Dock="Top">
            <WPF:RibbonFormattingTab x:Name="ribbonFormattingTab1" />
            <WPF:RibbonProofingTab x:Name="ribbonProofingTab1" />
        </Ribbon>
        <WPF:TextControl x:Name="textControl1"
                         RibbonFormattingTab="ribbonFormattingTab1"
                         RibbonProofingTab="ribbonProofingTab1"
                         SpellChecker="txSpellChecker1"
                         IsSpellCheckingEnabled="True" />
    </DockPanel>
</Window>

The XAML works as follows:

  • xmlns:spell maps the prefix spell to the namespace of TX Spell .NET. The prefix WPF is the one of TX Text Control.
  • <spell:TXSpellChecker x:Key="txSpellChecker1" /> creates the spell checker as a resource of the window.
  • SpellChecker holds the key of the resource as plain text, not a {StaticResource} reference. The TextControl looks the key up in its own resources and in those of its parents in the logical tree, such as the window. It initializes the connected spell checker itself.
  • RibbonProofingTab connects the Proofing tab by its name. Its buttons for spell checking, hyphenation, the thesaurus and the dictionaries work once a spell checker is connected.

Press F5 and type a misspelled word. It gets a red zigzag line, and a right-click shows suggestions:

A WPF TextControl with misspelled words underlined and the context menu of the word sampke with the suggestions sample, spake, smoke, samples and sampler and the commands Ignore, Ignore All, Add To Dictionary, Spell Check Dialog and Options.

Create Your First WPF Application in the TX Spell .NET documentation builds such a window step by step.

Several Editors, One Spell Checker

Every TextControl of the window can use the same resource: give each one SpellChecker="txSpellChecker1". The dictionaries are loaded once, and a word the user adds to the dictionary in one editor is accepted in all others. An editor created in code, as in Creating and Configuring Controls in Code, gets the key the same way, and code that needs the spell checker itself takes it from the resources:

using TXTextControl.WPF.Proofing;

// The spell checker of the window, declared in Window.Resources
TXSpellChecker txSpellChecker1 = (TXSpellChecker)FindResource("txSpellChecker1");

// A second editor uses the same spell checker through its key
TXTextControl.WPF.TextControl textControl2 = new TXTextControl.WPF.TextControl()
{
    SpellChecker = "txSpellChecker1",
    IsSpellCheckingEnabled = true
};

FindResource of the window returns the resource with the key. Add the new editor to a panel of the window, so that it finds the resource among the resources of its parents. Checking Multiple Controls also shows one spelling dialog for several editors.

Two samples of TX Spell .NET, in the folder Samples\WPF\CSharp\TextControl of its samples, share one spell checker resource between several editors: Multiple controls, and SpellCheckDialog on multiple controls, which shows four editors and a button that opens one spelling dialog for all of them:

A WPF sample window with four TextControls in a grid, all with underlined misspelled words, the context menu of one of them with suggestions for the word haeder, and a Spell Check Dialog button below.

Because the lookup goes up the logical tree, a spell checker in the resources of the window also serves editors inside the user controls of that window.

Deploy the Application

With the package, the build copies TXSpell.dll and TXSpell.WPF.dll and the folders Dictionaries, HyphenationLists, SynonymLists and LanguageRecognition into the output folder, and Publish into the publish folder. Deploy them with the files of TX Text Control, which Managed Assemblies and Native Libraries lists. The package contains US English only; Installing Dictionaries and Language Data adds other languages, and the Deployment Guide of TX Spell .NET covers the rest.

Where to Continue

TaskRead
Every member of the TextControl that works with TX Spell .NETTX Text Control .NET for WPF
Load dictionaries for other languagesLoading and Managing Dictionaries
Change the spelling dialog and the context menuSpell Check Dialog, Context Menus
Hyphenate documentsHyphenation with TX Text Control
Solve problems with dictionaries or licensesTroubleshooting

Next Steps