wpf - Context menu on any control on form -
how display context menu button in right-clicking on element on form?
the purpose of button in form be:
displaying x:name of control on right click(displaying context menu )is performed.
to summarize, want right click on element on form display context menu 1 button "show me name" should show messagebox displaying: "my name [x:name of element]"
define implicit style each type of control:
<window x:class="wpfapplication1.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:wpfapplication1" mc:ignorable="d" title="mainwindow" height="300" width="300"> <window.resources> <contextmenu x:key="cm"> <menuitem header="show name" click="menuitem_click" /> </contextmenu> <style targettype="button"> <setter property="contextmenu" value="{staticresource cm}" /> </style> <style targettype="textbox"> <setter property="contextmenu" value="{staticresource cm}" /> </style> </window.resources> <stackpanel> <button x:name="a" content="a" /> <textbox x:name="b" /> </stackpanel> </window> private void menuitem_click(object sender, routedeventargs e) { menuitem mi = sender menuitem; contextmenu cm = mi.parent contextmenu; frameworkelement fe = cm.placementtarget frameworkelement; messagebox.show(fe.name); }
Comments
Post a Comment