WPF Binding to IsEnabled Property in ListBox' ContextMenu's MenuItem -
i looking @ other threads regarding databinding contextmenus couldnt figure out how work since suggestions/answers wouldnt work me.
i have listbox, bound obversablecollection - works fine.
now have contextmenu inside listbox. contextmenu has 4 items activate, deactivate etc selected task (which item represented in listbox).
due permissions, need control, wether items in contextmenu enabled or disabled, have set isenabled-property of contextmenuitem binding same collection listbox bound to.
but reason, contextmenu items not getting disabled - property seems ignored.
edit: have implemented suggestion:
wpf
<listview margin="10,10,10,55" name="listviewcurrentjobs" itemssource="{binding jobcollection}"> <listview.contextmenu> <contextmenu> <menuitem header="starten" command="{binding path=startcommand}"/> <menuitem header="stoppen" command="{binding path=stopcommand}"/> <menuitem header="aktivieren" command="{binding path=enablecommand}"/> <menuitem header="deaktivieren" command="{binding path=disablecommand}"/> <menuitem header="löschen" command="{binding path=deletecommand}"/> </contextmenu> </listview.contextmenu> <listview.view> <gridview> <gridviewcolumn header="" width="32"> <gridviewcolumn.celltemplate> <datatemplate> <stackpanel orientation="horizontal"> <image source="{binding state}" width="16"/> </stackpanel> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> <gridviewcolumn header="name" displaymemberbinding="{binding description}" /> </gridview> </listview.view> </listview> c#
public class currentjob : monitoringwindow { public string state { get; set; } public string description { get; set; } public bool startpermitted { get; set; } public bool stoppermitted { get; set; } public bool enablepermitted { get; set; } public bool disablepermitted { get; set; } public bool deletepermitted { get; set; } public icommand startcommand { get; private set; } public icommand stopcommand { get; private set; } public icommand enablecommand { get; private set; } public icommand disablecommand { get; private set; } public icommand deletecommand { get; private set; } public currentjob() { startcommand = new delegatecommand(executestart, canexecutestart); stopcommand = new delegatecommand(executestop, canexecutestop); enablecommand = new delegatecommand(executeenable, canexecuteenable); disablecommand = new delegatecommand(executedisable, canexecutedisable); deletecommand = new delegatecommand(executedelete, canexecutedelete); } public bool canexecutestart() { return startpermitted; } public bool canexecutestop() { return stoppermitted; } public bool canexecuteenable() { return enablepermitted; } public bool canexecutedisable() { return disablepermitted; } public bool canexecutedelete() { return deletepermitted; } public void executestart() { currentjob curjob = ((currentjob)listviewcurrentjobs.selecteditem); string curjobname = curjob.name; if (new taskservice().getfolder("docuware notifications").tasks[curjobname].enabled == false) new taskservice().getfolder("docuware notifications").tasks[curjobname].enabled = true; new taskservice().getfolder("docuware notifications").tasks[curjobname].run(); loadjobs(); } public void executestop() { if (new taskservice().getfolder("docuware notifications").tasks[((currentjob)listviewcurrentjobs.selecteditem).name].enabled == true) { new taskservice().getfolder("docuware notifications").tasks[((currentjob)listviewcurrentjobs.selecteditem).name].stop(); loadjobs(); } } public void executeenable() { new taskservice().getfolder("docuware notifications").tasks[((currentjob)listviewcurrentjobs.selecteditem).name].enabled = true; loadjobs(); } public void executedisable() { new taskservice().getfolder("docuware notifications").tasks[((currentjob)listviewcurrentjobs.selecteditem).name].enabled = false; loadjobs(); } public void executedelete() { new taskservice().getfolder("docuware notifications").deletetask(((currentjob)listviewcurrentjobs.selecteditem).name); if (ismssql) { mssqlconn.open(); new sqlcommand("delete dbo.docuwarenotifications name = '" + ((currentjob)listviewcurrentjobs.selecteditem).name + "'", mssqlconn).executenonquery(); mssqlconn.close(); } else { mysqlconn.open(); new mysqlcommand("delete docuwarenotifications name = '" + ((currentjob)listviewcurrentjobs.selecteditem).name + "'", mysqlconn).executenonquery(); mysqlconn.close(); } loadjobs(); } } public partial class monitoringwindow : metrowindow { [...] foreach (task task in new taskservice().getfolder("docuware notifications").tasks) { if (task != null) { currentjob item = new currentjob(); switch (task.state) { case taskstate.disabled: item.state = "/dwnotdesigner;component/images/disabled.png"; item.description = task.name; break; case taskstate.ready: item.state = "/dwnotdesigner;component/images/active.png"; item.description = task.name; break; case taskstate.running: item.state = "/dwnotdesigner;component/images/working.png"; item.description = task.name; break; } item.startpermitted = startpermitted; item.stoppermitted = stoppermitted; item.enablepermitted = enablepermitted; item.disablepermitted = disablepermitted; item.deletepermitted = deletepermitted; _jobcollection.add(item); } } } for reason there no change!
you try using command bindings (you'll need prism):
... <contextmenu datacontext="{binding placementtarget.datacontext, relativesource={relativesource self}}"> <menuitem command="{binding startcommand}"/> ... </contextmenu> ... ...
using microsoft.practices.composite.presentation.commands; public class currentjob { public icommand startcommand {get; private set;} public currentjob () { startcommand = new delegatecommand(executestart, canexecutestart); } private bool canexecutestart() { //determine whether start can executed return true; } private void executestart() { //start here } } here, executestart replaces startjob , canexecutestart replaces enablepermitted in original example.
edit helps explain why original code not work. changes bound properties picked if "propertychange" event fired property's object. implementing inotifypropertychanged fix problem.
edit more trivial problem original code is binding wrong object - presume no property startpermitted exists on feedcontextmenu (which anyway doesn't seem defined).
edit there still few issues above: 1) bindings case-sensitive, binding should {binding startcommand} etc. 2) need set context menu on individual items in list. following work:
<listview margin="10,10,10,55" name="listviewcurrentjobs" itemssource="{binding jobcollection}"> <listview.itemtemplate> <datatemplate> <border borderbrush="black" borderthickness="2"> <stackpanel orientation="horizontal" minheight="20" background="white"> <stackpanel.contextmenu> <contextmenu datacontext="{binding placementtarget.datacontext, relativesource={relativesource self}}"> <menuitem header="starten" command="{binding path=startcommand}"/> <menuitem header="stoppen" command="{binding path=stopcommand}"/> <menuitem header="aktivieren" command="{binding path=enablecommand}"/> <menuitem header="deaktivieren" command="{binding path=disablecommand}"/> <menuitem header="löschen" command="{binding path=deletecommand}"/> </contextmenu> </stackpanel.contextmenu> <image source="{binding state}" width="16"/> </stackpanel> </border> </datatemplate> </listview.itemtemplate> </listview>
Comments
Post a Comment