c# - PlacementTarget of Contextmenu is not getting set -


this question has answer here:

my wpf application having button opens contextmenu upon clicking on it. using mvvm pattern , here xaml code.

in buttonclick() in viewmodel, isopenmenu set true. unable context menu correctly up.

<button content="click me" grid.column="1" name="btnview1" height="25" width="75" command="{binding buttonclick}" contextmenuservice.isenabled="false">     <button.contextmenu>         <contextmenu isenabled="true" isopen="{binding isopenmenu}" placementtarget="{binding elementname=btnview1}" placement="bottom" >             <menuitem header="menu 1" ischeckable="true"/>             <menuitem header="menu 1" ischeckable="true"/>             <menuitem header="menu 1" ischeckable="true"/>             <menuitem header="menu 1" ischeckable="true"/>         </contextmenu>     </button.contextmenu> </button> 

but when set 3 properties shown below in codebehind, getting correct results.

private void btnview1_click(object sender, routedeventargs e) {     (sender button).contextmenu.isenabled = true;     (sender button).contextmenu.placementtarget = (sender button);     (sender button).contextmenu.isopen = true; }   

i feel somehow placementtarget propery on contextmenu not getting set properly. doing wrong?

unfortunately xaml have written not work expect due how visual tree works, context menu not part of visual tree button in, reason not recognise name btnview1.

do need context menu appear in same place every time? if leave placement target blank should placed @ mouse click if recall correctly.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -