wpf - Change button image when context menu item is selected -
can tell me how change button image when context menu item clicked?
i have button image , context menu in it. want change image of button, everytime click contextmenu item. following piece of code able display contextmenu items on right click. don't know how proceed further. can guide me ? tried using command strangely command never got called.
<button background="gray" name="statusbtn" verticalalignment="top" horizontalalignment="right" fontweight="bold" foreground="red"> <dockpanel > <image dockpanel.dock="top" stretch="fill" source="{binding toenum, converter={staticresource enumtoimgconverter}}" height="37" width="72" /> <textblock horizontalalignment="center" margin="0,23,1,2">test</textblock> </dockpanel> <button.contextmenu> <contextmenu name="contextmenuname" itemssource="{binding path=popuplistitems}"> <contextmenu.itemtemplate > <datatemplate datatype="menuitem"> <menuitem header="{binding message}" command="{binding popuplistcommand}"> <menuitem.itemcontainerstyle > <style targettype="menuitem"> <setter property="ischeckable" value="true"/> <setter property="ischecked" value="{binding path=isselected, mode=twoway}"/> </style> </menuitem.itemcontainerstyle> </menuitem> </datatemplate> </contextmenu.itemtemplate> </contextmenu> </button.contextmenu> </button>
first, think need change menuitem.itemcontainerstyle menuitem.style - believe itemcontainerstyle meant sub-menuitems can place menuitem when want nested context menus.
i able content of button change following code. used text content, should easy swap out image content:
the xaml:
<window x:class="changebuttoncontent.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <grid> <button height="30" width="200"> <button.content> <dockpanel > <textblock text="{binding changingtext, mode=twoway}" /> </dockpanel> </button.content> <button.contextmenu> <contextmenu name="contextmenuname"> <menuitem command="{binding changetextcommand}" header="changetext"/> </contextmenu> </button.contextmenu> </button> </grid>
the code-behind:
public partial class mainwindow : window { public mainwindow() { initializecomponent(); this.datacontext = new viewmodel(); } } public class viewmodel : inotifypropertychanged { public viewmodel() { changingtext = "initial text"; changetextcommand = new mycommand((notused) => { changingtext = "text after context menu click"; }); } private string _changingtext; public string changingtext { { return _changingtext; } set { _changingtext = value; onpropertychanged("changingtext"); } } public mycommand changetextcommand { get; private set; } public event propertychangedeventhandler propertychanged; public void onpropertychanged(string propertyname) { var propertychanged = propertychanged; if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } } public class mycommand : icommand { private action<object> _action; public mycommand(action<object> action) { _action = action; } public bool canexecute(object parameter) { return true; } public event eventhandler canexecutechanged; public void execute(object parameter) { _action(parameter); } }
Comments
Post a Comment