TX Text Control .NET for WPF

Viewing, Zooming, and Navigation

Topic

Pages with margins or text that fills the window, a zoom factor, visible formatting marks and the page the user is on: these are view settings of the WPF TextControl. Several of them are dependency properties, so XAML sets and binds them like the properties of any WPF control. The sections below cover the view modes, zooming, formatting marks, moving to a page and following the position of the user.

Markup and code below go into a window with a TextControl named textControl1 in MainWindow.xaml, as in Create Your First WPF Application.

Choose a View Mode

The layout on the screen follows ViewMode:

  • PageView shows pages with gaps on a desktop background, formatted with the page size and margins, including headers and footers.
  • Normal formats the text with the page size and margins, but without the page view; headers and footers exist but aren't displayed.
  • FloatingText makes the text as wide as the control, with a vertical scroll bar for text of any length. Page size, margins, headers and footers are ignored.
  • SimpleControl formats the text to the width and height of the control, which accepts no more text once it is full.

Only in PageView does the editor count pages. Set the mode as an attribute in XAML:

<WPF:TextControl x:Name="textControl1" ViewMode="PageView" />

The scroll bars follow the WPF conventions: HorizontalScrollBarVisibility and VerticalScrollBarVisibility take a ScrollBarVisibility. Auto and Visible show a scroll bar when the document doesn't fit, and Disabled and Hidden never show one. The colors of the control are set with DisplayColors, a TextControlColors object.

Zoom the Document

ZoomFactor is the zoom factor in percent. Because it is a dependency property, a Slider or a TextBlock can be bound to it, and both follow when the user zooms in another way, for example with the status bar:

<TextBlock Text="{Binding ElementName=textControl1, Path=ZoomFactor, StringFormat={}{0} %}" />

Zoom With a Slider in Value Converters binds a slider through the LogarithmicConverter of TX Text Control, which puts 100 percent in the middle of the slider. In PageView and Normal mode, the factor can be from 10 up to ushort.MaxValue percent; in the other modes, from 10 to 400 percent.

In code, Zoom takes a factor or a value of ZoomOption: PageWidth fits the width of the page into the control, WholePage the current page and TextWidth the text of a line:

private void PageWidthButton_Click(object sender, RoutedEventArgs e)
{
    textControl1.Zoom(ZoomOption.PageWidth);
}

ZoomOption is in the TXTextControl namespace. Unlike the Windows Forms control, the WPF TextControl has no Zoomed event: bind to ZoomFactor instead, as the TextBlock above does.

Show Formatting Marks and Markers

The following properties show what doesn't appear on paper. ControlChars, TextFrameMarkerLines and DrawingMarkerLines are dependency properties:

PropertyShows
ControlCharsAll control characters, such as spaces, tabs and paragraph ends
PermanentControlCharsChosen control characters all the time, combined from PermanentControlChar values such as ParagraphEnd, ForcedPageBreak or SectionBreak
TextFrameMarkerLinesMarker lines around text frames without a border
DrawingMarkerLinesMarker frames around drawings
DocumentTargetMarkersMarkers at hypertext targets, such as bookmarks

A CheckBox bound to ControlChars lets the user switch the control characters on and off:

<CheckBox Content="Show control characters"
          IsChecked="{Binding ElementName=textControl1, Path=ControlChars}" />

The View tab of the ribbon, a RibbonViewTab that the TextControl finds by the name in its RibbonViewTab property, offers the views, the zoom factors and these marks without code.

The View tab of the WPF TX Text Control Words sample with the groups Document Views (Print Layout, Draft), Zoom (Zoom, 100 %, Full Page, Page Width, Text Width), Toolbars, Show and Application View.

Its Document Views group switches between Print Layout and Draft, the Zoom group sets the zoom factor or fits the page, its width or the text width into the control, and the Show group turns table gridlines, bookmark markers, marker lines, control characters and frame anchors on and off.

Go to a Page

A new InputPosition with a page, line and column moves the caret when it is assigned to the InputPosition property of the control. ScrollTo brings it into view:

private void GoToPageButton_Click(object sender, RoutedEventArgs e)
{
    int page = 3;

    // Pages exist only in page view
    if (textControl1.ViewMode != ViewMode.PageView || page > textControl1.Pages)
    {
        return;
    }

    // Line 1, column 0 is the start of the page
    textControl1.InputPosition = new InputPosition(page, 1, 0);
    textControl1.InputPosition.ScrollTo(InputPosition.ScrollPosition.Top);
    textControl1.Focus();
}

What the code does:

  • Pages is the number of pages of the document.
  • The InputPosition constructor counts pages and lines from 1 and columns from 0.
  • InputPosition.ScrollPosition Top scrolls the input position to the top of the visible area.
  • Focus gives the keyboard focus back to the editor after the click on the button.

Outside page view, GetPages returns null; in page view, it returns a PageCollection, and Page.Select selects the text of one page without its header and footer. The GotoDialog, a modeless window of the TXTextControl.WPF namespace, lets the user go to a page, section, line, table or bookmark:

private void GoToDialogButton_Click(object sender, RoutedEventArgs e)
{
    TXTextControl.WPF.GotoDialog dialog = new TXTextControl.WPF.GotoDialog(textControl1);
    dialog.Show();
}

Follow the Position in the Document

PageChanged occurs when the input position has moved to another page. Connect it with PageChanged="TextControl1_PageChanged" on the TextControl element; InputPosition.Page returns the page in page view:

private void TextControl1_PageChanged(object sender, EventArgs e)
{
    Title = $"Page {textControl1.InputPosition.Page} of {textControl1.Pages}";
}

The StatusBar of TX Text Control for WPF displays the page and the zoom factor without code; see Rulers and Status Bar. HScroll and VScroll report scrolling, and ScrollLocation is the upper-left corner of the visible part as a System.Windows.Point in twips.

Next Steps