Version

Showing a Contextual Tab Group Programmatically

Before You Begin

You can display context sensitive Tabs with the ContextualTabGroup object. However, simply adding a ContextualTabGroup to the ContextualTabGroupCollection will not be enough to show context sensitive Tabs. You will have to handle the GotFocus and LostFocus events of a control within your application to programmatically show or hide the ContextualTabGroup object.

What You Will Accomplish

By handling the GotFocus and LostFocus events of the controls in your Window, you will show or hide a ContextualTabGroup, respectively.

To Show a ContextualTabGroup Programmatically:

  1. Add a ContextualTabGroup to the ContextualTabGroups collection.

    1. Set the Caption property to "RichTextBox Tools".

    2. Set the Key property to "RichTextBox_Tabs".

    3. Set the IsVisible property to False.

  1. Add a RibbonTabItem to the ContextualTabGroup’s Tabs collection.

  2. Set the Header property of the RibbonTabItem to "Style".

  3. Add a second RibbonTabItem to the ContextualTabGroups Tab collection.

  4. Set the Header property of the second RibbonTabItem to "Behavior".

In XAML:

<igRibbon:XamRibbon Name="XamRibbon1">
...
    <igRibbon:XamRibbon.ContextualTabGroups>
        <igRibbon:ContextualTabGroup Caption="RichTextBox Tools"                                                      Key="RichTextBox_Tabs" IsVisible=">
            <igRibbon:RibbonTabItem Header="Style" />
            <igRibbon:RibbonTabItem Header="Behavior" />
        </igRibbon:ContextualTabGroup>
    </igRibbon:XamRibbon.ContextualTabGroups>
...
</igRibbon:XamRibbon>
  1. Add a RichTextBox control to the Window’s layout container. The XAML below assumes that you are using a StackPanel.

    1. Attach an event handler for the GotFocus event of the RichTextBox control.

    2. Attach an event handler for the LostFocus event of the RichTextBox control.

  1. Add a Button control to the Window’s layout container. This Button control will allow you to remove focus from the RichTextBox control.

In XAML:

...
<RichTextBox GotFocus="RichTextBox1_GotFocus"
  LostFocus="RichTextBox1_LostFocus" />
<Button Content="Remove focus from the RichTextBox control" />
...
  1. In the code-behind, create a method named RichTextBox1_GotFocus to handle the RichTextBox control’s GotFocus event.

  2. Create another method named RichTextBox1_LostFocus to handle the RichTextBox control’s LostFocus event.

  3. In the GotFocus event handler, use the key "RichTextBox_Tabs" to retrieve a reference to the ContextualTabGroup you added in step one.

  4. Set this ContextualTabGroup’s IsVisible property to True.

  5. In the LostFocus event handler, use the key "RichTextBox_Tabs" to retrieve a reference to the ContextualTabGroup you added in step one.

  6. Set this ContextualTabGroup’s IsVisible property to False.

In Visual Basic:

Private Sub RichTextBox1_GotFocus(ByVal sender As Object, _
  ByVal e As RoutedEventArgs)
    Me.XamRibbon1.ContextualTabGroups("RichTextBox_Tabs").IsVisible = True
End Sub
Private Sub RichTextBox1_LostFocus(ByVal sender As Object, _
  ByVal e As RoutedEventArgs)
    Me.XamRibbon1.ContextualTabGroups("RichTextBox_Tabs").IsVisible = False
End Sub

In C#:

private void RichTextBox1_GotFocus(object sender, RoutedEventArgs e)
{
        this.XamRibbon1.ContextualTabGroups["RichTextBox_Tabs"].IsVisible = true;
}
private void RichTextBox1_LostFocus(object sender, RoutedEventArgs e)
{
        this.XamRibbon1.ContextualTabGroups["RichTextBox_Tabs"].IsVisible = false;
}
  1. Run the application and place focus on the RichTextBox to show the Contextual Tabs related to RichTextBox tasks.

  2. Remove focus from the RichTextBox control by clicking on the Button control. This will hide the Contextual Tabs related to RichTextBox tasks.