开发者

WPF Dynamically add RibbonApplicationMenuItem(s)

I have the following code:

    <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" />                              
        &开发者_Python百科lt;/ribbon:RibbonApplicationMenu>
    </ribbon:Ribbon.ApplicationMenu>

I'd like to dynamically add items to my stack panel inside the first RibbonApplicationMenuItem replacing the TextBlock items that are hardcoded. I don't know how many will be available, I put 4 as an example.

Is this possible? If so, how do I go about doing it?

Thanks! Eroc


This is what I did in the XAML, I dropped the stack panel:

<!--<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>-->

And the code behind the form:

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

I hope this helps everyone, it seemed to work for me!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜