代码之家  ›  专栏  ›  技术社区  ›  dariusriggins

通过代码向WPF功能区添加新项

  •  4
  • dariusriggins  · 技术社区  · 17 年前

    我正在使用WPF Office功能区,并且我有一个内容视图,当该视图处于活动状态时,我希望向该功能区添加新项目。我有代码可以添加一个新的RibbonCommand和一个新的RibbonButton到我想要的组,但是添加它时不会发生任何事情。但是,如果我用按钮添加一个新组,它就会出现,并且被正确绑定。有什么方法可以让它更新我丢失的信息吗?我尝试过updateLayout(),但它也不起作用。我真的想尽量避免每次视图更改时重建所有组。

    作品:

    public void InjectItems(IView view)
    {
    var ribbonCommands = ProcessRibbonCommands(view.GetViewModel().Tasks, view.GetType());
    var group = new RibbonGroup();
    group.Command = new RibbonCommand() { LabelTitle = "Group Test" };            
    
    foreach (RibbonCommand command in ribbonCommands)
    {
        shell.MainRibbon.Resources.Add(command.Name, command);
        group.Controls.Add(new RibbonButton { Command = command });
    }
    
    shell.MainRibbon.SelectedTab.Groups.Add(group);
    }
    

    不起作用:

    public void InjectItems(IView view)
    {
    var ribbonCommands = ProcessRibbonCommands(view.GetViewModel().Tasks, view.GetType());
    var group = shell.MainRibbon.SelectedTab.Groups[0]; //I have a default group, will fix later
    
    foreach (RibbonCommand command in ribbonCommands)
    {
        shell.MainRibbon.Resources.Add(command.Name, command);
        group.Controls.Add(new RibbonButton { Command = command });
    }
    }
    
    2 回复  |  直到 15 年前
        1
  •  9
  •   Alastair Pitts    15 年前

    我假设您使用的是来自OfficeUI站点的Microsoft Ribbon CTP。

    作为许可协议的一部分,您需要遵循一些样式指导原则。其中之一是不根据当前视图添加/删除功能区的内容。

    来自DOC:

    组中显示的控件不得因选择而更改。如果控件未激活,则 控件必须灰显,而不是从组中删除。这提供了更可预测的 体验并防止功能区上控件的布局更改和分散用户的注意力。

    也就是说,听起来上下文选项卡正是您要查找的内容。可以禁用和启用这些选项,但选项卡的实际内容不会更改。

    这是在XAML中创建上下文选项卡的代码:

    <!--Context Groups-->
            <r:Ribbon.ContextualTabGroups>
                <!--Piece Contextual Group-->
                <r:RibbonContextualTabGroup x:Name="grpPieceContext" Label="Piece Tools">
                    <r:RibbonTab Label="Piece Information" Name="tabPieceContextInfo">
                        <r:RibbonGroup Name="grpPieceDetails" Command="{StaticResource PieceInformationGrpCommand}">
                            <r:RibbonLabel x:Name="lblPieceTag"/>
                            <r:RibbonTextBox Name="txtPieceDescription" Command="{StaticResource PieceNameTextboxCommand}" 
                                             TextChanged="txtPieceDescription_TextChanged" MaxLength="32"/>
                            <r:RibbonLabel x:Name="lblPieceLocation"/>
                        </r:RibbonGroup>
                    </r:RibbonTab>
                </r:RibbonContextualTabGroup>
            </r:Ribbon.ContextualTabGroups>
    

    然后,您可以通过以下代码激活和取消激活选项卡:

            if (!this.grpPieceContext.IsActive)
            {
                this.grpPieceContext.IsActive = true;
                this.grpPieceContext.Color = Colors.Orange;
            }
    

    其中,橙色是位于上下文组后面的颜色。

    希望这有帮助

        2
  •  1
  •   dariusriggins    17 年前

    最后我删除并重新创建了组和整个选项卡(如果需要)。