2009-04-10 88 views
4

向WPF Ribbon添加新項目我正在使用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 }); 
} 
} 
+0

你有沒有想過這個?我遇到了同樣的問題,無法通過代碼向現有組添加新項目。 – 2009-09-21 17:04:54

回答

9

我假設你正在使用微軟絲帶CTP從OfficeUI網站。

作爲許可協議的一部分,您需要遵循許多風格指南。其中之一是您不會根據當前視圖添加/刪除功能區的內容。

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

,其中橙色的顏色坐在上下文組的後面。

希望這有助於

+0

你不是你的! ``。 – 2011-02-17 10:40:41

1

我結束了剛剛刪除,如果需要重新創建組和整個標籤。