# Hyphenation With TX Text Control

> Hyphenate a WPF TextControl in XAML with SpellChecker and IsHyphenationEnabled, use the Proofing ribbon tab and text languages, and handle HyphenateWord.

- **Product:** TX Spell .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Spell .NET for WPF > Hyphenation > Hyphenation With TX Text Control
- **2340 words**
- **Web URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/hyphenation/tx-text-control/
- **LLMs URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/hyphenation/tx-text-control/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/hyphenation/tx-text-control/llms-full.txt

---

In a TX Text Control editor, TX Spell .NET divides the last word of a full line and moves the rest to the next line, as the user types and in every language that has a hyphenation list. Turning this on takes two properties. The sections below show what the user sees, how the language of the text chooses the rules, and how your code can change where a word is divided.

### What the User Sees

Once hyphenation is on, a word that doesn't fit at the end of a line is no longer moved to the next line as a whole. It is divided at a hyphenation point, and a hyphen marks the division:

![TX Text Control Words with a document whose lines end in divided words such as Win-dows, prop-erties and documenta-, with the Enable Hyphenation button of the ribbon switched on.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/hyphenation-in-words.f868d4d5.webp)

The division follows the text as it changes. While the user types, the word at the end of the line is divided as soon as it reaches the margin:

![Text being typed before the last word of a line in TX Text Control Words, which divides the word hyphenation as hyphen- when it reaches the right margin.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/hyphenation-while-typing.5963cca7.webp)

Both pictures show TX Text Control Words, the Windows Forms demo application of TX Text Control, in an earlier version of its ribbon. A `TextControl` in a WPF window hyphenates the same way.

### How It Works

Two properties of the `TextControl` are needed:

- [SpellChecker](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/spellchecker-property/llms-full.txt) connects a [TXSpellChecker](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/llms-full.txt). Its value is the key of the spell checker in the resources of the window. The same connection serves spell checking, as [As-You-Type Checking](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/as-you-type/llms-full.txt) shows.
- [IsHyphenationEnabled](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/ishyphenationenabled-property/llms-full.txt) turns hyphenation on.

When a word doesn't fit into a line, the spell checker calculates where to divide it, from the hyphenation list of the word's language. The `TextControl` then raises its [HyphenateWord](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/hyphenateword-event/llms-full.txt) event with that position, divides the word there and puts a hyphen at the end of the line.

### Turn On Hyphenation

Declare the spell checker as a resource of the window, and set both properties on the `TextControl` in *MainWindow.xaml*:

```xml
<Window x:Class="SpellingApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tx="clr-namespace:TXTextControl.WPF;assembly=TXTextControl.WPF"
        xmlns:spell="clr-namespace:TXTextControl.WPF.Proofing;assembly=TXSpell.WPF"
        Title="Hyphenation" Height="450" Width="800">
    <Window.Resources>
        <spell:TXSpellChecker x:Key="txSpellChecker1" />
    </Window.Resources>
    <tx:TextControl Name="textControl1"
                    SpellChecker="txSpellChecker1"
                    IsSpellCheckingEnabled="True"
                    IsHyphenationEnabled="True" />
</Window>
```

`SpellingApp` stands for the namespace of your project. In code, set `IsHyphenationEnabled` to `true` the same way, for example to let the user switch hyphenation with a check box.

Run the application and type a long text: the last word of each line is divided when it doesn't fit. [Create Your First WPF Application](https://docs.textcontrol.com/tx-spell-dotnet-wpf/getting-started/tx-text-control/first-application/llms-full.txt) sets up such an application from the start.

### Switch Hyphenation On and Off in the Ribbon

With the ribbon of TX Text Control, users switch hyphenation on and off themselves, with a button on the **Proofing** tab. The tab is a [RibbonProofingTab](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/ribbonproofingtab/llms-full.txt), connected to the `TextControl` through its [RibbonProofingTab](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/ribbonproofingtab-property/llms-full.txt) property. In TX Text Control Words, the Windows Forms demo, the button is **Enable Hyphenations** in the **Spelling & Hyphenation** group:

![The Proofing tab of the Windows Forms ribbon in TX Text Control Words with the buttons Enable Spell Checking, Enable Hyphenations, Thesaurus, Spelling, Load Dictionary, Load Thesaurus File and Manage Spelling & Hyphenation.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/ribbon-proofing-tab.748b5cc8.webp)

### Which List Hyphenates Which Text

Every part of a document can have a language of its own, and each word is hyphenated with the hyphenation list of its language. A document with English and German paragraphs therefore needs *hyph\_en\_US.dic* and *hyph\_de\_DE.dic* in the *HyphenationLists* folder; [Hyphenation Lists](https://docs.textcontrol.com/tx-spell-dotnet-wpf/hyphenation/lists/llms-full.txt) shows where to get them.

The user sets the language of text in the **Language** dialog or with the keyboard, and your code with `Selection.Culture`; [Multilingual Documents and Language Scopes](https://docs.textcontrol.com/tx-spell-dotnet-wpf/dictionaries/multilingual/#how-text-gets-a-language) describes these ways. With [IsLanguageDetectionEnabled](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/islanguagedetectionenabled-property/llms-full.txt) set to `true`, text without a language gets the language detected from the text, as [Language Detection](https://docs.textcontrol.com/tx-spell-dotnet-wpf/dictionaries/language-detection/llms-full.txt) shows.

The [HyphenationLanguage](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/hyphenationlanguage-property/llms-full.txt) property of the spell checker selects its default list. With its default value `"(Auto)"`, it follows the language of the spell check dictionary. The settings of each list, such as the fewest characters on each line, are changed by the user on the **Hyphenation** tab of the [Options Dialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/options-dialog/#the-hyphenation-and-thesaurus-tabs), and in code as [Hyphenation Lists](https://docs.textcontrol.com/tx-spell-dotnet-wpf/hyphenation/lists/#change-how-words-are-divided) shows.

### Change Where a Word Is Divided

The `HyphenateWord` event lets your code look at each division and change it. Its [HyphenateWordEventArgs](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/hyphenatewordeventargs/llms-full.txt) hold:

| Property | Contents |
|---|---|
| [Word](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/hyphenatewordeventargs/word-property/llms-full.txt) | The word that doesn't fit into the line |
| [Culture](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/hyphenatewordeventargs/culture-property/llms-full.txt) | The language of the word |
| [MaxDividePos](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/hyphenatewordeventargs/maxdividepos-property/llms-full.txt) | The largest position at which the word can be divided: with 3, at most the first three characters fit into the line |
| [DividePos](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/hyphenatewordeventargs/dividepos-property/llms-full.txt) | The position the spell checker calculated. Set it to divide the word elsewhere |

The `TextControl` divides the word at `DividePos` if it is not greater than `MaxDividePos`. This handler logs each division, which shows how the rules of a list work on your documents:

```csharp
private void textControl1_HyphenateWord(object sender, TXTextControl.HyphenateWordEventArgs e)
{
    System.Diagnostics.Debug.WriteLine(
        $"{e.Word} ({e.Culture}): divided after {e.DividePos} of at most {e.MaxDividePos} characters");
}
```

Attach it in XAML with `HyphenateWord="textControl1_HyphenateWord"` on the `TextControl`, or in code with `textControl1.HyphenateWord += textControl1_HyphenateWord;`. To divide a word by other rules, assign another position to `DividePos`. [GetHyphenationPoint](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/gethyphenationpoint-method/llms-full.txt) of the spell checker returns the last hyphenation point before a position, with the list of any language. This handler divides every word by the British English rules of *hyph\_en\_GB.dic*, which must be in the *HyphenationLists* folder:

```csharp
private void textControl1_HyphenateWord(object sender, TXTextControl.HyphenateWordEventArgs e)
{
    // Divide with the British English list, before the margin
    e.DividePos = txSpellChecker1.GetHyphenationPoint(e.Word, e.MaxDividePos, new System.Globalization.CultureInfo("en-GB"));
}
```

[Hyphenating Text in Code](https://docs.textcontrol.com/tx-spell-dotnet-wpf/hyphenation/in-code/llms-full.txt) explains `GetHyphenationPoint` and what it returns.

### Next Steps

- [Hyphenation Lists](https://docs.textcontrol.com/tx-spell-dotnet-wpf/hyphenation/lists/llms-full.txt): the files behind hyphenation, and their settings.
- [Hyphenating Text in Code](https://docs.textcontrol.com/tx-spell-dotnet-wpf/hyphenation/in-code/llms-full.txt): hyphenation points without a `TextControl`.
- [Multilingual Documents and Language Scopes](https://docs.textcontrol.com/tx-spell-dotnet-wpf/dictionaries/multilingual/llms-full.txt): documents in more than one language.
- [Options Dialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/options-dialog/llms-full.txt): the hyphenation settings users change.

---

## Nearby

- **Previous:** [Hyphenation Lists](https://docs.textcontrol.com/tx-spell-dotnet-wpf/hyphenation/lists/llms-full.txt)
- **Next:** [Hyphenating Text in Code](https://docs.textcontrol.com/tx-spell-dotnet-wpf/hyphenation/in-code/llms-full.txt)
