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

时间:2009-04-10 18:57:21

标签: c# wpf

我正在使用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 个答案:

答案 0 :(得分:9)

我假设你正在使用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;
        }

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

希望这有帮助

答案 1 :(得分:1)

我最后只是在必要时删除并重新创建组和整个标签。

相关问题