子菜单项未显示 [英] submenu items not shown

查看:76
本文介绍了子菜单项未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中的主菜单有问题,这些项是在运行时通过使用xaml文件中的模板和绑定到底层视图模型来构造的.显示所有顶级菜单项,但子菜单项从不显示..

这是mainwindow.xaml文件的一部分,其中定义了内容控制器:

I have a problem with the main menu in my application, the items are constructed run-time by using templates and bindings in the xaml file to underlaying viewmodels. All top level menu items are shown, but the submenu items never show up..

This is the part of the mainwindow.xaml file where the content controller is defined:

<ContentControl DockPanel.Dock="Top" Content="{Binding Path=MenuItems}" ContentTemplate="{StaticResource MainMenuTemplate}" />



这是资源字典的摘录,其中定义了用于构建菜单结构的不同模板:



This is a snippet from a resource dictionary where the different templates used to build the menu structure is defined:

<HierarchicalDataTemplate x:Key="ChildMenuItemTemplate" DataType="{x:Type vm:MenuItemViewModel}">
    <MenuItem Header="{Binding DisplayName}"

              Command="{Binding Path=Command}"

              ItemsSource="{Binding Path=Children}"

              Style="{DynamicResource MyMenuItemStyle}">
    </MenuItem>
</HierarchicalDataTemplate>

<DataTemplate x:Key="MainMenuTemplate">
    <Menu ItemsSource="{Binding}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate DataType="{x:Type vm:MenuItemViewModel}">
                <MenuItem Header="{Binding Path=DisplayName}"

                          ItemsSource="{Binding Path=Children}"

                          ItemTemplate="{DynamicResource ChildMenuItemTemplate}"

                          Style="{DynamicResource MyMenuItemStyle}">
                </MenuItem>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </Menu>
</DataTemplate>

推荐答案



我认为您正在定义层级模板,但没有为层级模板定义项目模板.为层级模板定义项目模板,然后尝试
例如:

Hi,

I think you are defining the hierachial template but you are not defining the item template for the hierarchial template.Defining the item template for the hierachial template and try,
eg:

<HierarchicalDataTemplate DataType="{x:Type local:Node}" ItemsSource="{Binding Path=Children}">
           <MenuItem Header="{Binding DisplayName}"

             Command="{Binding Path=Command}"

             ItemsSource="{Binding Path=Children}"

             Style="{DynamicResource MyMenuItemStyle}">
           </MenuItem>

           <HierarchicalDataTemplate.ItemTemplate>
               <DataTemplate DataType="{x:Type local:Node}">
                   <TextBlock HorizontalAlignment="Stretch" Text="{Binding NodeName}" Width="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Width}"/>
               </DataTemplate>
           </HierarchicalDataTemplate.ItemTemplate>
       </HierarchicalDataTemplate>


这篇关于子菜单项未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆