WPF 动态添加 RibbonApplicationMenuItem(s)

发布于 2024-10-01 07:29:16 字数 1942 浏览 3 评论 0原文

我有以下代码:

    <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个为例。

这可能吗?如果是这样,我该如何去做呢?

谢谢! 埃罗克

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" />                              
        </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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

又怨 2024-10-08 07:29:16

这就是我在 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);      
  }

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

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文