dynamic WPF ContextMenu -
i have wpf , dynamic contextmenu. have menuitem1.click , menuitem2.click.
contextmenu contextmenu = new contextmenu(); menuitem mitem1 = new menuitem() { header = "mitem1" }; mitem1.click += new system.windows.routedeventhandler(mitem1_click); menuitem mitem2 = new menuitem() { header = "mitem2" }; mitem2.click += new ystem.windows.routedeventhandler(mitem2_click);
now when call
private void mitem_click(object sender, routedeventargs e) { //need put mitem1.enabled=false , mitem2.enabled = false }
how do this? use contextmenu.items , what? `
since creating menuitem
objects dynamically, keep reference them using 2 private fields. can access them directly in event handlers:
public partial class mainwindow : window { menuitem mitem1; menuitem mitem2; public mainwindow() { initializecomponent(); contextmenu contextmenu = new contextmenu(); mitem1 = new menuitem() { header = "mitem1" }; mitem1.click += new system.windows.routedeventhandler(mitem1_click); mitem2 = new menuitem() { header = "mitem2" }; mitem2.click += new system.windows.routedeventhandler(mitem2_click); } private void mitem1_click(object sender, routedeventargs e) { mitem1.isenabled = false; } private void mitem2_click(object sender, routedeventargs e) { } }
another option may use anonymous methods:
contextmenu contextmenu = new contextmenu(); menuitem mitem1 = new menuitem() { header = "mitem1" }; menuitem mitem2 = new menuitem() { header = "mitem2" }; mitem1.click += (ss, ee) => { mitem2.isenabled = false; }; mitem2.click += (ss, ee) => { };
Comments
Post a Comment