65.9K
CodeProject 正在变化。 阅读更多。
Home

WinForms Ribbon控件,第17部分 – 上下文选项卡

starIconstarIconstarIconstarIconstarIcon

5.00/5 (11投票s)

2010年3月2日

Ms-PL

2分钟阅读

viewsIcon

25613

downloadIcon

1677

在本文中,我将介绍如何使用 ribbon 上下文选项卡。

这系列 CodeProject 文章基于我首先在我的 博客上发表的一系列帖子。

WinForms Ribbon控件 库现在支持上下文选项卡。 这篇文章的结果是项目网站上的另一个示例,“14-ContextualTabs”。

image

什么是上下文选项卡?

上下文选项卡是在启用其上下文时出现的附加选项卡。 例如,在 Word 中,当您在文档中选择表格时,会获得两个附加选项卡(“设计”和“布局”),其中包含仅与表格相关的命令。

基本工作单元是 TabGroup,它是一组具有相同上下文的上下文选项卡。 TabGroup 具有一个名为 ContextAvailable(属性标识符:UI_PKEY_ContextAvailable)的属性,该属性可以具有以下值:

  • Active - 上下文当前可用,选项卡组应处于活动状态(即“选中”)。
  • Available - 上下文当前可用(选项卡不一定处于活动状态)。
  • NotAvailable - 上下文当前不可用。

有关此主题的更多详细信息,请参阅 MSDN 上的 显示上下文选项卡

使用 ContextualTabs - Ribbon 标记

以下是定义上下文选项卡的 views 部分。 commands 部分很简单。

<Application.Views>
  <Ribbon>
    <Ribbon.ContextualTabs>
      <TabGroup CommandName='cmdTabGroupTableTools'>
        <Tab CommandName='cmdTabDesign'>
          <Group CommandName='cmdGroupDesign' SizeDefinition='ThreeButtons'>
            <Button CommandName='cmdButtonDesign1' />
            <Button CommandName='cmdButtonDesign2' />
            <Button CommandName='cmdButtonDesign3' />
          </Group>
        </Tab>
        <Tab CommandName='cmdTabLayout'>
          <Group CommandName='cmdGroupLayout' SizeDefinition='TwoButtons'>
            <Button CommandName='cmdButtonLayout1' />
            <Button CommandName='cmdButtonLayout2' />
          </Group>
        </Tab>
      </TabGroup>
    </Ribbon.ContextualTabs>
    <Ribbon.Tabs>
      <Tab CommandName='cmdTabMain'>
        <Group CommandName='cmdGroupMain' SizeDefinition='TwoButtons'>
          <Button CommandName='cmdButtonSelect' />
          <Button CommandName='cmdButtonUnselect' />
        </Group>
      </Tab>
    </Ribbon.Tabs>
  </Ribbon>
</Application.Views>

在这里,我们为“表格工具”定义了一个 TabGroup,其中包含两个上下文选项卡“设计”和“布局”。 每个选项卡都包含一些按钮。 此外,我们在主选项卡中定义了两个按钮,我们将使用它们来设置和取消设置“表格工具”上下文。

使用 ContextualTabs - 代码隐藏

以下是设置 TabGroup 上下文的示例,从而使其可见:

private Ribbon _ribbon;
private RibbonTabGroup _tabGroupTableTools;
private RibbonButton _buttonSelect;
private RibbonButton _buttonUnselect;

public Form1()
{
    InitializeComponent();

    _ribbon = new Ribbon();
    _tabGroupTableTools = new RibbonTabGroup(_ribbon, 
                            (uint)RibbonMarkupCommands.cmdTabGroupTableTools);
    _buttonSelect = new RibbonButton(_ribbon, 
                            (uint)RibbonMarkupCommands.cmdButtonSelect);
    _buttonUnselect = new RibbonButton(_ribbon, 
                            (uint)RibbonMarkupCommands.cmdButtonUnselect);

    _buttonSelect.OnExecute += new OnExecuteEventHandler(_buttonSelect_OnExecute);
    _buttonUnselect.OnExecute += new OnExecuteEventHandler(_buttonUnselect_OnExecute);
}

void _buttonSelect_OnExecute(PropertyKeyRef key, PropVariantRef currentValue, 
                             IUISimplePropertySet commandExecutionProperties)
{
    _tabGroupTableTools.ContextAvailable = ContextAvailability.Active;
}

void _buttonUnselect_OnExecute(PropertyKeyRef key, PropVariantRef currentValue, 
                               IUISimplePropertySet commandExecutionProperties)
{
    _tabGroupTableTools.ContextAvailable = ContextAvailability.NotAvailable;
}

在这里,我们只是注册到 ribbon 按钮的 Execute 事件,并相应地设置 TabGroup 的上下文。 当然,在实际应用程序中,您可能使用更复杂的逻辑来设置上下文。

目前就到这里为止。

© . All rights reserved.