WPF MenuItem 子项不显示

发布于 2024-07-22 21:20:51 字数 830 浏览 1 评论 0原文

我正在使用 ObjectDataProvider 和 DataTemplate 来填充菜单栏中的 MenuItem。 (WPF、C#/XAML)请参阅下面的片段。

结果:出现顶部菜单项,当我单击它时,会出现环绕菜单项(带有绑定标题文本的菜单项)以及指示子项存在的小箭头,但悬停或单击箭头不会显示子项,无法访问它们。

预期结果:孩子们看得见,行为正常。

片段:

<ObjectDataProvider x:Key="Brokers" ObjectInstance="{x:Static brokers:BrokerManager.Instance}" MethodName="GetBrokers" IsAsynchronous="True" />
        <DataTemplate x:Key="BrokerMenuItem" DataType="IBroker">
            <MenuItem Header="{Binding Path=Name}">
                <MenuItem Header="Connect" />
                <MenuItem Header="Disconnect" />
            </MenuItem>
        </DataTemplate>

<MenuItem Header="Brokers" ItemsSource="{Binding Source={StaticResource Brokers}}" ItemTemplate="{DynamicResource BrokerMenuItem}"/>

I am using an ObjectDataProvider and a DataTemplate to populate a MenuItem inside my Menu bar. (WPF, C#/XAML) See snipet below.

Result: The top menu item appears, when i click on it, the wrapping menu item (the one with the bound header text) appears along with the little arrow indicating the presence of children but hovering or clicking the arrow does not show the children, they cannot be accessed.

Expected result: The children are visible and behave properly.

Snippet:

<ObjectDataProvider x:Key="Brokers" ObjectInstance="{x:Static brokers:BrokerManager.Instance}" MethodName="GetBrokers" IsAsynchronous="True" />
        <DataTemplate x:Key="BrokerMenuItem" DataType="IBroker">
            <MenuItem Header="{Binding Path=Name}">
                <MenuItem Header="Connect" />
                <MenuItem Header="Disconnect" />
            </MenuItem>
        </DataTemplate>

<MenuItem Header="Brokers" ItemsSource="{Binding Source={StaticResource Brokers}}" ItemTemplate="{DynamicResource BrokerMenuItem}"/>

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

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

发布评论

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

评论(3

亚希 2024-07-29 21:20:51

arsemrkt:我有完全相同的问题,如果我使用 DataTemplate 填充 MenuItem,我似乎无法将子项添加到任何这些生成的项目中。 我不明白你的答案,我应该如何使用 ContentPresenter 来解决这个问题?

编辑:
实际上,我的问题并不完全相同,因为我试图将集合的集合绑定到菜单。 我想我已经通过使用 HierarchicalDataTemplate 让它工作了:

<Menu>
    <MenuItem Header="{Binding Name}" ItemsSource="{Binding MenuOptions}">
        <MenuItem.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Categories}">
                <MenuItem Header="{Binding Name}"/>
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <MenuItem Header="{Binding Name}"/>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </MenuItem.ItemTemplate>
    </MenuItem>
</Menu>

这对你有帮助吗 NicholasF?

arsenmrkt: I have exactly the same problem, if I populate a MenuItem using a DataTemplate I cant seem to add children to any of those generated items. I don't understand your answer though, how should I use the ContentPresenter to get around this problem?

EDIT:
Actually, my problem was'nt exactly the same, since I'm trying to bind a collection of collections to a menu. I think I've gotten it to work though using the HierarchicalDataTemplate:

<Menu>
    <MenuItem Header="{Binding Name}" ItemsSource="{Binding MenuOptions}">
        <MenuItem.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Categories}">
                <MenuItem Header="{Binding Name}"/>
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <MenuItem Header="{Binding Name}"/>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </MenuItem.ItemTemplate>
    </MenuItem>
</Menu>

Does this help you NicholasF?

尹雨沫 2024-07-29 21:20:51

经过一周多的搜索,我终于找到了如何使其正常工作。 事实证明,数据模板对于动态菜单来说效果不太好。 执行此操作的正确方法是使用 MenuItem 的 ItemContainerStyle 属性。 (或者是 ItemStyleContainer?)

只需创建一个样式来覆盖标题并将其设置为您需要的任何内容。 我他们重写了 ItemsSource 以包含我的孩子。 但这里要小心,因为子级将继承样式,并且每个子级都有相同的子级并生成递归菜单。 您需要覆盖子项的 ItemsSource 并将其设置为空 x:Array 或类似内容。

有几个博客描述了如何使用 ItemContainerStyle,请查看它们。

After searching for over a week, i finally found how to make this work properly. It turns out DataTemplates don't work too great for dynamic menus. The proper way to do this is to use the ItemContainerStyle property of MenuItem. (Or is that ItemStyleContainer?)

Simply create a style to override the header and set it to whatever you need. I them overrode the ItemsSource to include my children. However be careful here, as the children will inherit the style and each have the same children and generate a recursive menu. You'll need to override the ItemsSource of your children and set it to an empty x:Array or the likes.

There are several blogs out there describing how to use ItemContainerStyle, check them out.

淡墨 2024-07-29 21:20:51

menuitem 控件的 ItemSource 属性用于为该项目提供子项,请尝试将 与该数据模板一起使用。

ItemSource property of menuitem control is used for giving childes for that item, try to use <ContentPresenter /> with that datatemplate.

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