简体   繁体   中英

Cascaded ContextMenu in WPF ComboBox

I want to create a ComboBox that lets users select items the way the Windows XP start menu allows to select programs:

Is there a way to insert a cascaded ContextMenu inside a ComboBox ?

I tried this but it doesn't work properly:

<ComboBox>
        <MenuItem Header="Top Level 1">
            <MenuItem Header="Sub Level" />
            <MenuItem Header="Sub Level" />
        </MenuItem>
        <MenuItem Header="Top Level 2">
            <MenuItem Header="Sub Level" />
            <MenuItem Header="Sub Level" />
        </MenuItem>
</ComboBox>

You will want to try somthing like this, The next level must be inside the first level.

<Menu Margin="0,0,0,283">
    <MenuItem Header="Top Level 1">
        <MenuItem Header="Sub Level" />
        <MenuItem Header="Sub Level" />
        <MenuItem Header="Top Level 2">
            <MenuItem Header="Sub Level" />
            <MenuItem Header="Sub Level" />
            <MenuItem Header="Top Level 3">
                <MenuItem Header="Sub Level" />
                <MenuItem Header="Sub Level" />
            </MenuItem>
        </MenuItem>
    </MenuItem>
</Menu>

在此处输入图片说明

Do you have to use a ComboBox?

Using the Menu control will make your MenuItems work easily... so I would use that instead: http://wpftutorial.net/Menus.html

You can always alter the look of it as well. There are some examples here: http://www.c-sharpcorner.com/uploadfile/mahesh/menus-in-wpf/

Else, did you try to wrap your menuitems in a simply?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM