wpf - Bind to parent control from within context menu within data template -
i'm wanting bind parent control context menu within datatemplate.
unfortunately i'm restricted .net 3.5 , can't use x:reference extension introduced in .net 4.
below example of i'm trying do
<window x:class="wpfapplication17.window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfapplication17" name="window"> <window.resources> <datatemplate datatype="{x:type local:car}"> <rectangle width="100" height="100" fill="red"> <rectangle.contextmenu> <contextmenu> <menuitem header="{binding colour}"/> <menuitem header="{binding elementname=window, path=actualwidth}"/> </contextmenu> </rectangle.contextmenu> </rectangle> </datatemplate> </window.resources> </window>
but "cannot find source binding reference 'elementname=window'" error due context menu not being part of visual tree.
edit :
that works great! .. however, doesn't seem work when use composite collection such following
<window.resources> <datatemplate datatype="{x:type local:car}"> <rectangle width="100" height="100" fill="red" tag="{binding relativesource={relativesource ancestortype={x:type window}}}"> <rectangle.contextmenu> <contextmenu> <contextmenu.itemssource> <compositecollection> <menuitem header="{binding colour}"/> <menuitem header="{binding path=placementtarget.tag.actualwidth, relativesource={relativesource ancestortype=contextmenu}}"/> </compositecollection> </contextmenu.itemssource> </contextmenu> <!--<contextmenu> <menuitem header="{binding colour}"/> <menuitem header="{binding path=placementtarget.tag.actualwidth, relativesource={relativesource ancestortype=contextmenu}}"/> </contextmenu>--> </rectangle.contextmenu> </rectangle> </datatemplate> </window.resources>
please try this:
<datatemplate datatype="{x:type local:car}"> <rectangle width="100" height="100" fill="red" tag="{binding relativesource={relativesource ancestortype={x:type window}}}"> <rectangle.contextmenu> <contextmenu> <menuitem header="{binding colour}"/> <menuitem header="{binding path=placementtarget.tag.actualwidth, relativesource={relativesource ancestortype=contextmenu}}"/> </contextmenu> </rectangle.contextmenu> </rectangle> </datatemplate>
see answer here too.
Comments
Post a Comment