WPF动态添加RibbonApplicationMenuItem(s)

时间:2010-11-06 18:00:09

标签: c# wpf ribbon-control

我有以下代码:

    <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

1 个答案:

答案 0 :(得分: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);      
  }

我希望这对每个人都有帮助,它似乎对我有用!