繁体   English   中英

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

[英]Adding new items to WPF Ribbon via Code

我正在使用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 });
}
}

我假设您正在使用OfficeUI站点中的Microsoft Ribbon CTP。

作为许可协议的一部分,您可以遵循许多样式指南。 其中之一是您不会根据当前视图添加/删除功能区的内容。

来自文档:

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

话虽如此,听起来像一个上下文选项卡正是你正在寻找的。 这些可以被禁用和启用,但选项卡的实际内容不会更改。

这是在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是位于上下文组后面的颜色。

希望这可以帮助

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM