c# - Why binding MenuItems to a ContextMenu doesn't display anything? -


i'm beginner in mvvm , i've been trying display menuitems in contextmenu, seems wrong. i've made working menu has itemssource bound collection in viewmodel, doesn't work in case of contextmenu. guys take , if i'm missing something?

here's menu, works well:

<menu dockpanel.dock="right" itemssource="{binding path=modes}" background="#ff303030" foreground="white" height="21">     <menu.resources>         <style targettype="{x:type menuitem}">             <setter property="background" value="#ff303030"/>             <setter property="foreground" value="white"/>             <setter property="height" value="21" />             <setter property="visibility" value="{binding visibility}" />             <setter property="header" value="{binding header}" />             <setter property="itemssource" value="{binding children}"/>             <setter property="command" value="{binding action}"/>             <setter property="commandparameter" value="{binding attacheddata}"/>         </style>     </menu.resources> </menu> 

and here's xaml of contextmenu, which, reason, doesn't display menuitems:

<grid x:name="btnmode" previewmousedown="onbtnmodemousedown" width="19" height="19" margin="0,0" dockpanel.dock="right">     <canvas>         <path data="m0,0 l19,0 l19,19 l0,19" margin="0,0" fill="#ff303030"/>         <ellipse height="12" margin="4,3,0,0" width="12" fill="#fffdfdfd"/>         <path data="m1.5,1l18.5,1 l10,9.5" margin="0,0,0,8.875" fill="#ff303030"/>     </canvas>     <grid.contextmenu>         <contextmenu itemssource="{binding path=modes}">             <contextmenu.itemcontainerstyle>                 <style targettype="menuitem">                     <setter property="background" value="#ff303030"/>                     <setter property="foreground" value="white"/>                     <setter property="height" value="21" />                     <setter property="visibility" value="{binding visibility}" />                     <setter property="header" value="{binding header}" />                     <setter property="itemssource" value="{binding children}"/>                     <setter property="command" value="{binding action}"/>                     <setter property="commandparameter" value="{binding attacheddata}"/>                 </style>             </contextmenu.itemcontainerstyle>         </contextmenu>     </grid.contextmenu> </grid> 

i have contextmenu has hard-coded menu items:

<grid x:name="btnmenu" previewmousedown="onbtnmenumousedown" width="19" height="19" margin="0,1" dockpanel.dock="right">     <canvas>         <path data="m0,0 l19,0 l19,19 l0,19" margin="0,0" fill="#ff303030"/>         <path data="m0,0l7,0 l3.5,7" margin="6,7" fill="white"/>     </canvas>     <grid.contextmenu >         <contextmenu isvisiblechanged="onbtnmenupopup">             <menuitem name="menufloatingwindow" header="floating window" click="ondockingmenu"></menuitem>             <menuitem name="menudockedwindow" header="docked window" click="ondockingmenu"></menuitem>             <menuitem name="menutabbeddocument" header="tabbed document" click="ondockingmenu"></menuitem>             <menuitem name="menuautohide" header="auto hide" click="ondockingmenu"></menuitem>             <menuitem name="menuclose" header="close" click="ondockingmenu"></menuitem>         </contextmenu>     </grid.contextmenu> </grid> 

the collection of menuitems observablecollection, defined follows:

_menus = new observablecollection<menuitem>() {     new menuitem(         "file",                // string header         visibility.visible,    // visibility visibility         null,                  // icommand action         null,                  // object attacheddata (commandparameters)         new observablecollection<menuitem>()   // children         {             new menuitem(                 "exit",                 visibility.visible,                 mainwindow.exitcommand,                 null,                 null             )         }     ),     ...,  // more nested menuitems here }; 

both working menu , contextmenu have same observablecollection itemsource.

here's image showing how controls in application:

menus

did mess in xaml?

p.s. tried debugging xaml @ runtime using this: http://www.wpftutorial.net/debugdatabinding.html couldn't see helpful information when breakpoint occured.


Comments