wpf - How to make a custom button display a menu -


i have custom button in wpf want use main menu button style reasons. trying create menu similar of microsoft office 2007. i've added menu items button, menu not open when click on button. in fact, compiler error says content has been set many times. think may have image. other code need add or change in button make open menu when clicked, error free?

this have under button right now:

<!--- main menu button -->         <button width="50"         height="50" horizontalalignment="left" margin="12,12,0,0" verticalalignment="top">             <!--- main menu button image -->             <button.content>                 <stackpanel orientation="horizontal">                     <image                         source="..."                                 width="40"                                height="40" />                 </stackpanel>             </button.content>             <!-- menu command -->             <menu>                 <menuitem x:name="mainmenu">                     <menuitem header="new" />                     <menuitem header="open" />                     <menuitem header="exit" click="exit_click" />                 </menuitem>             </menu>          </button> 

i've read examples talk binding, not sure how in specific instance.

thank you.

you can use routedevent button.click, show contextmenu:

<button name="mainbutton" content="button contextmenu" width="150" height="30">     <button.contextmenu>         <contextmenu x:name="maincontextmenu" placementrectangle="{binding relativesource={relativesource self}}">             <menuitem header="main">                 <menuitem header="find" />                 <menuitem header="add" />                 <menuitem header="view" />                 <menuitem header="edit" />             </menuitem>         </contextmenu>     </button.contextmenu>      <button.triggers>         <eventtrigger sourcename="mainbutton" routedevent="button.click">             <beginstoryboard>                 <storyboard>                     <objectanimationusingkeyframes storyboard.targetname="maincontextmenu" storyboard.targetproperty="(contextmenu.isopen)">                         <discreteobjectkeyframe keytime="0:0:0">                             <discreteobjectkeyframe.value>                                 <sys:boolean>true</sys:boolean>                             </discreteobjectkeyframe.value>                         </discreteobjectkeyframe>                     </objectanimationusingkeyframes>                 </storyboard>             </beginstoryboard>         </eventtrigger>     </button.triggers> </button> 

output

enter image description here

note: add sys namespace that:

xmlns:sys="clr-namespace:system;assembly=mscorlib" 

to window:

<window x:class="showcontextmenu.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:sys="clr-namespace:system;assembly=mscorlib"     title="mainwindow" height="350" width="525" .../> 

Comments