Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new items to WPF Ribbon via Code

Tags:

c#

wpf

I'm using the WPF Office Ribbon, and I have a content view that I would like to have add new items to the ribbon when that view becomes active. I have code that adds a new RibbonCommand as well as a new RibbonButton to the group I want, but nothing happens when I add it. However, if I add a new group with the button, it comes up fine and is bound correctly. Is there some method to get it to update that I'm missing? I've tried UpdateLayout() and it does not work either. I'd really like to try and avoid rebuilding all the groups everytime the view changes.

Works:

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);
}

Doesn't Work:

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 });
}
}
like image 249
dariusriggins Avatar asked Dec 30 '22 02:12

dariusriggins


2 Answers

I'm assuming you're using the Microsoft Ribbon CTP from the OfficeUI site.

As part of the licensing agreement there are a number of style guidelines that you are expected to follow. One of them is that you don't add/remove contents of the Ribbon based on your current view.

from the doc:

Controls displayed in a group MUST NOT change as a result of selection. If a control is not active, the control MUST be grayed out, rather than removed from the group. This provides a more predictable experience and prevents the layout of controls on the Ribbon from changing and distracting users.

that being said, it sounds like a Context tab is exactly what you're looking for. These can be disabled and enabled but the actual contents of the tab don't change.

This is the code to create a context tab in 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>

you can then active and de-active the tab via this code:

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

where orange is the color that sits in behind the context group.

Hope this helps

like image 146
Alastair Pitts Avatar answered Jan 09 '23 02:01

Alastair Pitts


I ended up just deleting and recreating the group and entire tab if necessary.

like image 40
dariusriggins Avatar answered Jan 09 '23 02:01

dariusriggins