Howto: Manipulate the MiniToolbar

This chapter explains the typical process of manipulating the MiniToolbar.

The source code for this example is contained in the following directories:

  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for WPF\CSharp\Howto\MiniToolbar
  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for WPF\VB.NET\Howto\MiniToolbar

Used TX Text Control controls:

  • TXTextControl.WPF.TextControl

Relevant API links:

This sample shows how to manipulate the MiniToolbar in order to add contextual content groups.

Inspired by Microsoft Word, the new MiniToolbar is a small version of the full ribbon toolbar providing the most common formatting features. The MiniToolbar appears when text is selected or selected text is right-clicked.

Image

This context sensitive toolbar is very handy for quick formatting tasks without moving the mouse back and forth from the selection to the full ribbon bar.

When right-clicking a selection or an object such as a table or text frame, contextual features are displayed in the MiniToolbar.

The TextControl.ShowMiniToolbar property can be used to control under which circumstances the toolbar should be shown. By default, the MiniToolbar is not shown and must be enabled programmatically to keep applications backwards compatible. In the TextControl.MiniToolbarOpening event, the MiniToolbar can be customized or replaced.

The sample adds a new group to the MiniToolbar to edit hyperlinks when the MiniToolbar is opened by clicking a hyperlink in a document. The following code creates a new RibbonGroup:

private Grid CreateEditHyperlinkGroup(int column) {
        // Create a ribbon group (represented by a Grid) that contains...
        Grid rgEditHyperlinkGroup = new Grid();
        Grid.SetRow(rgEditHyperlinkGroup, 0);
        Grid.SetRowSpan(rgEditHyperlinkGroup, 3);
        Grid.SetColumn(rgEditHyperlinkGroup, column);

        rgEditHyperlinkGroup.ColumnDefinitions.Add(new ColumnDefinition() {
                Width = GridLength.Auto
        });

        // ... a button to open the TextControl Edit HyperlinkDialog
        RibbonButton rbtnEditHyperlinkButton = new RibbonButton() {
                Label = "Edit Hyperlink"
        };
        Grid.SetRow(rbtnEditHyperlinkButton, 0);
        Grid.SetColumn(rbtnEditHyperlinkButton, 0);

        rbtnEditHyperlinkButton.LargeImageSource = TXTextControl.WPF.ResourceProvider.GetLargeIcon(TXTextControl.WPF.RibbonInsertTab.RibbonItem.TXITEM_InsertHyperlink.ToString(), this);
        rbtnEditHyperlinkButton.Click += EditHyperlink_Click;

        // Add the edit hyperlink button to group.
        rgEditHyperlinkGroup.Children.Add(rbtnEditHyperlinkButton);

        return rgEditHyperlinkGroup;
}
Private Function CreateEditHyperlinkGroup(ByVal column As Integer) As Grid
        ' Create a ribbon group (represented by a Grid) that contains...
        Dim rgEditHyperlinkGroup As Grid = New Grid()
        Grid.SetRow(rgEditHyperlinkGroup, 0)
        Grid.SetRowSpan(rgEditHyperlinkGroup, 3)
        Grid.SetColumn(rgEditHyperlinkGroup, column)

        rgEditHyperlinkGroup.ColumnDefinitions.Add(New ColumnDefinition() With {
                        .Width = GridLength.Auto
                })

        ' ... a button to open the TextControl Edit HyperlinkDialog
        Dim rbtnEditHyperlinkButton As RibbonButton = New RibbonButton() With {
                        .Label = "Edit Hyperlink"
                }
        Grid.SetRow(rbtnEditHyperlinkButton, 0)
        Grid.SetColumn(rbtnEditHyperlinkButton, 0)

        rbtnEditHyperlinkButton.LargeImageSource = TXTextControl.WPF.ResourceProvider.GetLargeIcon(TXTextControl.WPF.RibbonInsertTab.RibbonItem.TXITEM_InsertHyperlink.ToString(), Me)
        AddHandler rbtnEditHyperlinkButton.Click, AddressOf EditHyperlink_Click

        ' Add the edit hyperlink button to group.
        rgEditHyperlinkGroup.Children.Add(rbtnEditHyperlinkButton)

        Return rgEditHyperlinkGroup
End Function

This group is added in the TextControl.TextMiniToolbarInitialized event:

private void TextControl_TextMiniToolbarInitalized(object sender, TXTextControl.WPF.MiniToolbarInitializedEventArgs e) {
        // Ensure that the TextMiniToolbar's table layout group won't be displayed if the input position is inside a table.
        e.MiniToolbar.Container.Children.Remove(e.MiniToolbar.Container.FindName(TXTextControl.WPF.TextMiniToolbar.RibbonItem.TXITEM_TableLayoutGroup.ToString()) as UIElement);

        // Add a ribbon group separator.
        e.MiniToolbar.Container.Children.Add(CreateRibbonGroupSeperator(3));

        // Create and add a ribbon group to the TextMiniToolbar that provides an "Edit Hyperlink" button.
        e.MiniToolbar.Container.ColumnDefinitions.Add(new ColumnDefinition() {
                Width = GridLength.Auto
        });
        e.MiniToolbar.Container.Children.Add(CreateEditHyperlinkGroup(4));
}
Private Sub TextControl_TextMiniToolbarInitalized(ByVal sender As Object, ByVal e As TXTextControl.WPF.MiniToolbarInitializedEventArgs)
        ' Ensure that the TextMiniToolbar's table layout group won't be displayed if the input position is inside a table.
        e.MiniToolbar.Container.Children.Remove(TryCast(e.MiniToolbar.Container.FindName(TXTextControl.WPF.TextMiniToolbar.RibbonItem.TXITEM_TableLayoutGroup.ToString()), UIElement))

        ' Add a ribbon group separator.
        e.MiniToolbar.Container.Children.Add(CreateRibbonGroupSeperator(3))

        ' Create and add a ribbon group to the TextMiniToolbar that provides an "Edit Hyperlink" button.
        e.MiniToolbar.Container.ColumnDefinitions.Add(New ColumnDefinition() With {
                        .Width = GridLength.Auto
                })
        e.MiniToolbar.Container.Children.Add(CreateEditHyperlinkGroup(4))
End Sub

In the TextControl.MiniToolbarOpening event, contextual groups are hidden dynamically:

private void TextControl_MiniToolbarOpening(object sender, TXTextControl.WPF.MiniToolbarOpeningEventArgs e) {
        // Check whether the opening mini tool bar is of type TextMiniToolbar
        if (e.MiniToolbar is TXTextControl.WPF.TextMiniToolbar) {
                e.MiniToolbar.Container.Children[2].Visibility = System.Windows.Visibility.Visible; // Ensure that the TextMiniToolbar's Styles group is always shown (even if the input position is inside a table)
                e.MiniToolbar.Container.Children[3].Visibility = // Ensure that the ribbon group's separator and...
                e.MiniToolbar.Container.Children[4].Visibility =            // ... the "Edit Hyperlink" group are displayed if...

                (e.MiniToolbarContext & TXTextControl.ContextMenuLocation.TextField) == TXTextControl.ContextMenuLocation.TextField && // ... the current context is TextField and ...
                m_txTextControl.HypertextLinks.GetItem() != null ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed; // ... the text field is of type TXTextControl.HypertextLink.
        }
}
Private Sub TextControl_MiniToolbarOpening(ByVal sender As Object, ByVal e As TXTextControl.WPF.MiniToolbarOpeningEventArgs)
        ' Check whether the opening mini tool bar is of type TextMiniToolbar
        If TypeOf e.MiniToolbar Is TXTextControl.WPF.TextMiniToolbar Then
                e.MiniToolbar.Container.Children(2).Visibility = Visibility.Visible ' Ensure that the TextMiniToolbar's Styles group is always shown (even if the input position is inside a table)

                e.MiniToolbar.Container.Children(3).Visibility = CSharpImpl.Assign(e.MiniToolbar.Container.Children(4).Visibility, If((e.MiniToolbarContext And TXTextControl.ContextMenuLocation.TextField) = TXTextControl.ContextMenuLocation.TextField AndAlso Me.m_txTextControl.HypertextLinks.GetItem() IsNot Nothing, Visibility.Visible, Visibility.Collapsed)) ' Ensure that the ribbon group's separator and...
                ' ... the "Edit Hyperlink" group are displayed if...
                ' ... the current context is TextField and ...
                ' ... the text field is of type TXTextControl.HypertextLink.
        End If
End Sub

When right-clicking a hyperlink in the sample, the MiniToolbar is shown with the new contextual ribbon group:

Image