c# - Reuse context menu -
i have created context menu (at moment) use items in treeview. have created treeitem
class holds relevant information header, icon, children, execute target, etc. looks like:
<contextmenu datacontext="{binding placementtarget.datacontext, relativesource={relativesource self}}" visibility="{binding showcontextmenu}" itemssource="{binding contextmenu}"> <contextmenu.itemtemplate> <hierarchicaldatatemplate itemssource="{binding children}"> <textblock text="{binding header}" /> <hierarchicaldatatemplate.itemcontainerstyle> <style targettype="menuitem"> <setter property="command" value="{binding execute}" /> <setter property="icon" value="{staticresource cmicon}" /> <setter property="tooltip" value="{binding tooltip}" /> </style> </hierarchicaldatatemplate.itemcontainerstyle> </hierarchicaldatatemplate> </contextmenu.itemtemplate> <contextmenu.itemcontainerstyle> <style targettype="menuitem"> <setter property="command" value="{binding execute}" /> <setter property="icon" value="{staticresource cmicon}" /> <setter property="tooltip" value="{binding tooltip}" /> </style> </contextmenu.itemcontainerstyle> </contextmenu>
when used context menu in tree view, attached textblock in itemtemplate. want use same context menu different control. don't want copy same code different location , maintain multiple times, want reuse template. tried 2 things:
i put context menu in resources of user control (just testing) , call this:
<textblock text="{binding header}" contextmenu="{staticresource mycontextmenu}">
. displayed, not closed , not move. not helpful anyway want use context menu on different user control.then put context menu inside control template in app.xaml:
<controltemplate x:key="treecontextmenu" targettype="contextmenu">
. , call this:<textblock.contextmenu> <contextmenu template="{staticresource treecontextmenu}"/> </textblock.contextmenu>
the program starts, when want open context menu, exception: 'contextmenu' cannot have logical or visual parent.
i have tried google solution, couldn't find helpful.
you trying create context menu inside context menu. remove controltemplate
tag app.xaml , move x:key
attribute directly contextmenu
tag.
also, delete textblock.contextmenu
, add contextmenu="{staticresource treecontextmenu}"
attribute textblock
tag.
Comments
Post a Comment