xaml - DataBinding to a context menu in wpf? -
i have usercontrol, defined so:
<usercontrol x:name=usercontrolname> <usercontrol.resources> <style targettype="{x:type mycontrol}"> <setter property="contextmenu"> <setter.value> <contextmenu > <menuitem header="item" command="{binding elementname=usercontrolname, path=deletecommand"> </menuitem> </contextmenu> </setter.value> </setter> <setter property="template"> <setter.value> <controltemplate targettype="{x:type controltype}"> <grid> <!--mycontenthere--> </grid> </controltemplate> </setter.value> </setter> </style> </usercontrol.resources> <grid> <!--content --> <listbox> <listboxitem> <!--is of type mycontrol--> </listbox> </grid> </usercontrol> this not work, usercontrolname's datacontext not being found.
am missing here?
you should try using wpf relativesource binding such:
<contextmenu > <menuitem header="item" command="{binding relativesource={relativesource ancestortype={x:type usercontrol}}, path=datacontext.deletecommand}"> </menuitem> </contextmenu>
Comments
Post a Comment