2010-11-06 73 views
1

我有以下代碼:WPF動態添加RibbonApplicationMenuItem(S)

<ribbon:Ribbon.ApplicationMenu> 
     <ribbon:RibbonApplicationMenu ToolTipTitle="Application Menu"> 
      <ribbon:RibbonApplicationMenuItem Header="Company" 
               x:Name="MenuItem_Company" 
               ImageSource="Images\LargeIcon.png" 
               Command="{Binding Path=CompanyCommand}"> 
       <StackPanel>        
        <TextBlock Text="Item 1 in the list" /> 
        <TextBlock Text="Item 2 in the list" /> 
        <TextBlock Text="Item 3 in the list" /> 
        <TextBlock Text="Item 4 in the list" /> 
       </StackPanel> 
      </ribbon:RibbonApplicationMenuItem> 

      <ribbon:RibbonApplicationMenuItem Header="DocStore Settings" 
               x:Name="MenuItem1" 
               ImageSource="Images\LargeIcon.png" 
               Click="MenuItem1_Click"/> 
      <ribbon:RibbonApplicationMenuItem Header="About DocStore" 
               x:Name="MenuItem2" 
               ImageSource="Images\LargeIcon.png" 
      Click="MenuItem2_Click" /> 
      <ribbon:RibbonApplicationMenuItem Header="Exit" 
               x:Name="MenuExit" 
               ImageSource="Images\LargeIcon.png" 
      Click="Exit" />        
     </ribbon:RibbonApplicationMenu> 
    </ribbon:Ribbon.ApplicationMenu> 

我想動態地添加項目到我的堆疊面板第一RibbonApplicationMenuItem替換是硬編碼將TextBlock裏面的物品。我不知道有多少可用,我以4爲例。

這可能嗎?如果是這樣,我該如何去做呢?

謝謝! Eroc

回答

0

這是我在XAML一樣,我放棄了堆疊面板:

<!--<StackPanel x:Name="CompanyStackPanel">        
    <TextBlock Text="Item 1 in the list" /> 
    <TextBlock Text="Item 2 in the list" /> 
    <TextBlock Text="Item 3 in the list" /> 
    <TextBlock Text="Item 4 in the list" /> 
</StackPanel>--> 

和窗體後面的代碼:

// ToDo: Create interface to populate the mymenutems 
    List<string> mymenuitems = new List<string>(); // = someinterface 
    mymenuitems.Add("Test Menu 1"); 
    mymenuitems.Add("Test Menu 2"); 
    mymenuitems.Add("Test Menu 3"); 
    mymenuitems.Add("Test Menu 4"); 
    foreach (var item in mymenuitems) 
    { 
    var margins = new Thickness(2); 
    var newtextbox = new Label() { Margin = margins, Content = item}; 
    MenuItem_Company.Items.Add(newtextbox);  
    } 

我希望這有助於大家,它似乎爲我工作!