Events
The WPF TextControl has two kinds of events. Those it inherits from System.Windows.Controls.Control, such as Loaded, KeyDown or GotFocus, are WPF routed events. The more than 130 events that TX Text Control adds, from Changed to TrackedChangeCreated, are ordinary .NET events of the control. Both kinds are connected in XAML or in code. Here is how, followed by the events grouped by subject and two common features built from them.
Everything below builds on a window with a TextControl named textControl1 in MainWindow.xaml, as in Create Your First WPF Application.
How the Events Work
A handler receives sender, the TextControl, and an argument with the details of the event:
- Changed, InputPositionChanged, PageChanged and many others pass a plain
EventArgs; the handler reads the current state from the control. - Events about an object pass an argument class that contains it, such as TrackedChangeEventArgs or ImageEventArgs. These classes are in the
TXTextControlnamespace, which the WPF product shares with the other TX Text Control products. - A few argument classes let the handler decide what the editor does, for example CannotTrackChangeEventArgs with
CancelandHandled.
Because the events of TX Text Control aren't routed events, they don't bubble up to the window: connect them on the TextControl itself.
Connect an Event Handler
Name the handler in an attribute of the TextControl element in MainWindow.xaml. Visual Studio offers to create the method in MainWindow.xaml.cs:
<WPF:TextControl x:Name="textControl1"
Loaded="TextControl1_Loaded"
Changed="TextControl1_Changed"
DocumentLoaded="TextControl1_DocumentLoaded"
ContentsReset="TextControl1_ContentsReset"
InputPositionChanged="TextControl1_InputPositionChanged" />
In code, connect a handler with += and disconnect it with -=, for example textControl1.Changed += TextControl1_Changed; in the constructor after InitializeComponent. Loaded is the WPF event that signals that the control is ready: put code that works with the document there, such as loading a file.
Track Unsaved Changes
Changed occurs when the contents of the document have changed, DocumentLoaded when a document has been loaded, and ContentsReset when all contents have been deleted and reset. With the handlers of the XAML above, they keep a flag up to date:
private bool documentModified;
private void TextControl1_Changed(object sender, EventArgs e)
{
documentModified = true;
}
private void TextControl1_DocumentLoaded(object sender, EventArgs e)
{
// A freshly loaded document has no unsaved changes
documentModified = false;
}
private void TextControl1_ContentsReset(object sender, EventArgs e)
{
documentModified = false;
}
The handlers of TX Text Control events take an EventArgs, the Loaded handler a RoutedEventArgs. Reset the flag after your code has saved the document, too. Load and Save Documents in Getting Started uses such a flag to ask about unsaved changes before New, Open and closing the window, where the Closing handler sets e.Cancel to keep the window open.
Show the Position of the Caret
InputPositionChanged occurs after each move of the input position. The following handler writes the line and column into a TextBlock named positionText:
private void TextControl1_InputPositionChanged(object sender, EventArgs e)
{
InputPosition position = textControl1.InputPosition;
positionText.Text = $"Line {position.Line}, column {position.Column}";
}
CharFormatChanged and ParagraphFormatChanged report changed formatting at the input position or in the selection, for example to update a toggle button of your own from Selection.Bold. The status bar of TX Text Control shows the position without code; see Rulers and Status Bar.
Events by Subject
Object events come in families of created, deleted, selected, clicked, moved and sized:
In the assembly of version 34.0, the two mini toolbar events are compiled as TextMiniToolbarInitalized and ObjectMiniToolbarInitalized, without the second "i"; attach handlers with these names, as Toolbars and Mini Toolbars explains.
Compared with the Windows Forms control, the WPF TextControl has no Zoomed event and no Chart... events. To follow the zoom factor, bind to the ZoomFactor dependency property, as Viewing, Zooming, and Navigation shows.
Handle Events in a Derived Control
Each event has a protected method that raises it, On followed by the event name, for example OnChanged. A class derived from the WPF TextControl overrides it and calls the base method, which raises the event for the connected handlers:
using System;
public class AuditTextControl : TXTextControl.WPF.TextControl
{
public DateTime LastChange { get; private set; }
protected override void OnChanged(EventArgs e)
{
LastChange = DateTime.Now;
// Raise the Changed event for the connected handlers
base.OnChanged(e);
}
}
Next Steps
- Architecture and Control Lifecycle: when the control is ready and how it is set up.
- Selections and Caret Position: the input position that many events report.
- Track Changes and Comments: events of a review workflow in use.
- TextControl: the members of the editor at a glance.