在 WPF 功能区应用程序菜单中使用 DataTemplate 时会出现附加嵌套级别

发布于 2024-10-17 05:20:18 字数 2055 浏览 1 评论 0原文

我正在使用 Microsoft 提供的 WPF 功能区控件

问题在于,当我使用 DataTemplate 填充 RibbonApplicationSplitMenuItem 时,我得到了一个我认为不应该存在的额外嵌套级别。

这是相关的 WPF 代码:

<Window.Resources>
    <DataTemplate DataType="{x:Type cfg:PluginInfoConfigurationElement}" x:Key="GotoPluginAppMenuItem">
        <ribbon:RibbonApplicationMenuItem 
                                Header="{Binding Path=Key}"
                                ImageSource="{Binding Path=Image}"/>
    </DataTemplate>
</Window.Resources>

            <ribbon:RibbonApplicationMenu>
                <ribbon:RibbonApplicationSplitMenuItem x:Name="LoadPluginMenuItem" 
                   ItemsSource="{Binding Source={StaticResource NlpModel}, Path=AvailablePlugins}" 
                   Header="Plugins"
                   ItemTemplate="{StaticResource GotoPluginAppMenuItem}">
                </ribbon:RibbonApplicationSplitMenuItem>
                <ribbon:RibbonApplicationSplitMenuItem x:Name="LoadPluginMenuItem2" 
                                                       Header="Plugins">
                    <ribbon:RibbonApplicationMenuItem 
                                Header="FooPlugin"
                                ImageSource="Images/icon-32.png"/>
                    <ribbon:RibbonApplicationMenuItem 
                                Header="Invalid"
                                ImageSource="Images/icon-32.png"/>
                </ribbon:RibbonApplicationSplitMenuItem>
                <!-- Other items to fill the menu -->
            </ribbon:RibbonApplicationMenu>

这是我得到的:

带有数据模板使用数据模板。

正如您所看到的,使用 DataTemplate 时会出现额外的嵌套级别。我怎样才能防止这种情况发生?

I am using the WPF Ribbon control provided by Microsoft.

The problem consists in that, when I use a DataTemplate to fill a RibbonApplicationSplitMenuItem, I get an additional nesting level that I believe should not be there.

Here is the relevant WPF code:

<Window.Resources>
    <DataTemplate DataType="{x:Type cfg:PluginInfoConfigurationElement}" x:Key="GotoPluginAppMenuItem">
        <ribbon:RibbonApplicationMenuItem 
                                Header="{Binding Path=Key}"
                                ImageSource="{Binding Path=Image}"/>
    </DataTemplate>
</Window.Resources>

            <ribbon:RibbonApplicationMenu>
                <ribbon:RibbonApplicationSplitMenuItem x:Name="LoadPluginMenuItem" 
                   ItemsSource="{Binding Source={StaticResource NlpModel}, Path=AvailablePlugins}" 
                   Header="Plugins"
                   ItemTemplate="{StaticResource GotoPluginAppMenuItem}">
                </ribbon:RibbonApplicationSplitMenuItem>
                <ribbon:RibbonApplicationSplitMenuItem x:Name="LoadPluginMenuItem2" 
                                                       Header="Plugins">
                    <ribbon:RibbonApplicationMenuItem 
                                Header="FooPlugin"
                                ImageSource="Images/icon-32.png"/>
                    <ribbon:RibbonApplicationMenuItem 
                                Header="Invalid"
                                ImageSource="Images/icon-32.png"/>
                </ribbon:RibbonApplicationSplitMenuItem>
                <!-- Other items to fill the menu -->
            </ribbon:RibbonApplicationMenu>

And here is what I get:

with a DataTemplate

With a Data Template.

what I would like

Without the template.

As you can see, an additional nesting level appears when using a DataTemplate. How can I prevent that?

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

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

发布评论

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

评论(1

紅太極 2024-10-24 05:20:18

您需要设置 ItemContainerStyle 而不是设置 ItemTemplate,否则您最终会在 ribbon:RibbonApplicationMenuItem 内部得到一个 ribbon:RibbonApplicationMenuItem

让·霍米纳尔
这是我使用的代码,它达到了我想要的结果:

<Style TargetType="{x:Type ribbon:RibbonApplicationMenuItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <ribbon:RibbonApplicationMenuItem Header="{Binding Path=Caption}"
                                                  ImageSource="{Binding Path=Image}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Instead of setting the ItemTemplate you need to set ItemContainerStyle otherwise you are ending up with a ribbon:RibbonApplicationMenuItem inside of the ribbon:RibbonApplicationMenuItem.

Jean Hominal:
Here is the code that I have used, which achieved the result I wanted:

<Style TargetType="{x:Type ribbon:RibbonApplicationMenuItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <ribbon:RibbonApplicationMenuItem Header="{Binding Path=Caption}"
                                                  ImageSource="{Binding Path=Image}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文