wpf - ContextMenu.ItemSource binding issue -


i have static resource defined follows:

    <contextmenu x:key="testcontextmenu" datacontext="{binding path=placementtarget, relativesource={x:static relativesource.self}}">         <menuitem command="applicationcommands.cut"/>         <!--<contextmenu.itemssource>             <compositecollection>                 <menuitem command="applicationcommands.cut"/>             </compositecollection>         </contextmenu.itemssource>-->     </contextmenu> 

my application works fine this. however, want able add items context menu. instead of adding menu items want use compositecollection , run binding issues. minimized problem , ended this. when comment menuitem , uncomment contextmenu.itemsource error:

system.windows.data error: 4 : cannot find source binding reference 'relativesource findancestor, ancestortype='system.windows.controls.itemscontrol', ancestorlevel='1''. bindingexpression:path=horizontalcontentalignment; dataitem=null; target element 'menuitem' (name=''); target property 'horizontalcontentalignment' (type 'horizontalalignment')

if datacontext correct in first situation why not correct anymore in second situation?

edit: don't want add items contextmenu dynamically. want use contextmenu kind of 'base context menu' providing cut/copy , paste composite collection resource. in places want more these 3 , there use custom context menu use same collection combined items. clarify xaml have in mind, cut problem down simpler piece above.

    <compositecollection x:key="treeviewitemcontextmenuitems">         <menuitem command="applicationcommands.cut" commandtarget="{binding}"/>         <menuitem command="applicationcommands.copy" commandparameter="{binding}"/>         <menuitem command="applicationcommands.paste" commandparameter="{binding}"/>         <separator/>         <menuitem command="applicationcommands.delete" commandparameter="{binding}"/>     </compositecollection>     <contextmenu x:key="treeviewitemcontextmenu" datacontext="{binding path=placementtarget, relativesource={x:static relativesource.self}}">         <contextmenu.itemssource>             <compositecollection>                 <collectioncontainer collection="{staticresource treeviewitemcontextmenuitems}" />             </compositecollection>         </contextmenu.itemssource>     </contextmenu> 

if want dynamically populate contextmenu better solution follows:

<contextmenu      x:key="testcontextmenu"     itemssource="{binding menuitems}">         </contextmenu> 

i removed datacontext="{binding path=placementtarget...}}" , added itemssource="{binding menuitems}". menuitems property of object used data context. here example of usage:

<window>     ...     <itemscontrol contextmenu="{staticresource resourcekey=testcontextmenu}">         ...     </itemscontrol> </window 

in case context menu inherit data context itemscontrol in turn inherit data context window.

if don't want remove datacontext="{binding path=placementtarget...}}" use following code:

<contextmenu      x:key="testcontextmenu"     datacontext="{binding path=placementtarget, relativesource={x:static relativesource.self}}"     itemssource="{binding datacontext.menuitems}">         </contextmenu> 

edit:

you receive these errors because menuitem controls default try bind of properties (horizontalcontentalignment , verticalcontentalignment) itemscontrol using relativesource. problem embeded inside compositecollection doesn't support kind of binding - see this article.

the problem occure if override binding in xaml in way:

<menuitem command="applicationcommands.cut" commandtarget="{binding}"      horizontalcontentalignment="center"  verticalcontentalignment="center"/> 

probably there workaround of problem i'll ignore these errors. don't spoil in application, don't they?


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -