简体   繁体   中英

How to expand sub menus from code behind

This is XAML codes.

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <Menu x:Name="Menu1">
        <MenuItem x:Name="MenuItem1" Height="40" Header="File">
            <MenuItem x:Name="MenuItem11" Header="Help"/>
            <MenuItem x:Name="MenuItem12" Header="About"/>
            <MenuItem x:Name="MenuItem13" Header="Exit"/>
        </MenuItem>
    </Menu>
</Grid>
</Window>

How to expand sub menus from code behind?

Question picture: https://prnt.sc/img9p6

You will need a MenuViewModel class that has a ObservalbleCollection of SubMenuViewModel(s).

This class will need a property for the IsOpen for The Menu. https://msdn.microsoft.com/en-us/library/system.windows.controls.menuitem.issubmenuopen(v=vs.110).aspx

You should not create the MenuItems in WPF. But bind the Itemssource to thies collection.

You then bind the IsOpen from the MenuItem to the MenuItems in WPF (ItemsTemplate)

WPF - How can I create menu and submenus using binding

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