wpf - Dynamic Mvvm ContextMenu but SubMenu not showing -
the code snippet below works desired of couple posts here, no visual flaws more , incl. separator. tried add sub-contextmenu itemsource, , not working.
this have, said working expected, code snipped window 3 section 2 not (test submenu).
next xaml (isnullconverter1 converter test object not null).
<listbox itemssource="{binding stackvisualitems}" selecteditem="{binding selectedvisualitem}"> <listbox.itemtemplate> <datatemplate datatype="models:visualitem"> <textblock text="{binding text2display}" /> </datatemplate> </listbox.itemtemplate> <listbox.contextmenu> <contextmenu itemssource="{binding actioncollection}"> <contextmenu.resources> <controltemplate x:key="menuseparatortemplate"> <separator /> </controltemplate> <controltemplate x:key="dynamicmenutemplate"> <contentpresenter content="{binding}"> <contentpresenter.contenttemplate> <datatemplate datatype="wpf:contextmenuviewmodel"> <menuitem command="{binding actioncommand, targetnullvalue={x:null}, fallbackvalue={x:null}}" commandparameter="{binding actionparameter, fallbackvalue={x:null}, targetnullvalue={x:null}}" header="{binding displayname, targetnullvalue='targetnullvalue', fallbackvalue='fallbackvalue'}" itemssource="{binding itemsources, fallbackvalue={x:null}, targetnullvalue={x:null}}"> <menuitem.icon> <image width="16" height="16" source="{binding path=icon, fallbackvalue={x:null}, targetnullvalue={x:null}}" /> </menuitem.icon> </menuitem> </datatemplate> </contentpresenter.contenttemplate> </contentpresenter> </controltemplate> </contextmenu.resources> <contextmenu.itemcontainerstyle> <style targettype="menuitem"> <style.triggers> <datatrigger binding="{binding}" value="{x:null}"> <setter property="template" value="{staticresource menuseparatortemplate}" /> </datatrigger> <datatrigger binding="{binding path=., converter={staticresource isnullconverter1}}" value="false"> <setter property="template" value="{staticresource dynamicmenutemplate}" /> </datatrigger> </style.triggers> </style> </contextmenu.itemcontainerstyle> </contextmenu> </listbox.contextmenu> </listbox>
the contextmenuviewmodel looks this, trivial , nothing special:
public class contextmenuviewmodel : viewmodelbase { private string _displayname; private icommand _actioncommand; private imagesource _icon; private object _actionparameter; private observablecollection<contextmenuviewmodel> _itemsources; public contextmenuviewmodel(string displayname) { displayname = displayname; } public contextmenuviewmodel(string displayname, icommand contextmenuactioncommand) : this(displayname) { actioncommand = contextmenuactioncommand; } public contextmenuviewmodel(string displayname, icommand contextmenuactioncommand, imagesource icon) : this(displayname, contextmenuactioncommand) { icon = icon; } public string displayname { { return _displayname; } set { _displayname = value; raisepropertychanged(() => displayname); } } public icommand actioncommand { { return _actioncommand; } set { _actioncommand = value; raisepropertychanged(() => actioncommand); } } public imagesource icon { { return _icon; } set { _icon = value; raisepropertychanged(() => icon); } } public object actionparameter { { return _actionparameter; } set { _actionparameter = value; raisepropertychanged(() => actionparameter); } } public observablecollection<contextmenuviewmodel> itemsources { { return _itemsources; } set { _itemsources = value; raisepropertychanged(() => itemsources); } } }
the dynamic contextmenu build way, incl. adding separator null value:
private void setupdynamiccontextmenu() { imagesource copy = new bitmapimage(new uri(@"pack://application:,,,/resources/copy_16.png", urikind.absolute)); imagesource cut = new bitmapimage(new uri(@"pack://application:,,,/resources/cut_16.png", urikind.absolute)); imagesource delete = new bitmapimage(new uri(@"pack://application:,,,/resources/delete_16.png", urikind.absolute)); actioncollection = new observablecollection<contextmenuviewmodel>(); actioncollection.add(new contextmenuviewmodel("copy all", new relaycommand<string>(copyallcommand, o => cancopyall()), copy)); //does not work: submenu actioncollection.add(new contextmenuviewmodel("test submenu")); actioncollection[actioncollection.count - 1].itemsources = new observablecollection<contextmenuviewmodel>(); actioncollection[actioncollection.count - 1].itemsources.add(new contextmenuviewmodel("copy test", new relaycommand<string>(copyallcommand, o => true), cut)); actioncollection.add(null); //separator actioncollection.add(new contextmenuviewmodel("copy current", new relaycommand<string>(copycurrentcommand, o => true), cut)); //actioncollection.add(new contextmenuviewmodel("copy selection", new relaycommand<string>(copyselectioncommand, o => true))); }
Comments
Post a Comment