C#/MVVM: Enable/Disable Buttons based on another control's property -
say have tab control displays data of various types, eg editortabviewmodel
, previewtabviewmodel
both inheriting tabviewmodel
. implementation similar tutorial on msdn
i want enable buttons depending on active tab, whether editortabviewmodel
or previewtabviewmodel
. how can achieve this?
update
public icommand editorcommand { { if (_editorcommand == null) { _editorcommand = new relaycommand(() => { messagebox.show("editor"); }, () => { var enabled = true; var viewsource = collectionviewsource.getdefaultview(tabs); viewsource.currentchanged += (o, e) => { if (viewsource.currentitem editortabviewmodel) { enabled = false; } }; return enabled; }); } return _editorcommand; } }
update 2
public icommand previewcommand { { if (_previewcommand == null) { _previewcommand = new relaycommand(() => { messagebox.show("preview"); }, () => { var viewsource = collectionviewsource.getdefaultview(tabs); var enabled = viewsource.currentitem editortabviewmodel; viewsource.currentchanged += (o, e) => { commandmanager.invalidaterequerysuggested(); }; return enabled; }); } return _previewcommand; } }
i suggest create icommand
implementation constructs on icollectionview
contains 2 tab controls. command can react currentchanged
event collection view determine whether or not should enabled, , raise canexecutechanged
event indicate change.
class mycommand : icommand { private bool _isenabled = true; public mycommand(mytoplevelviewmodel viewmodel) { var viewsource = collectionviewsource.getdefaultview(viewmodel.tabs); viewsource.currentchanged += (o,e) => { _isenabled = (viewsource.currentitem editortabviewmodel); //or want decide if (this.canexecutechanged != null) this.canexecutechanged(this, eventargs.empty); }; } public void execute(object parameter) { /*...*/ } public bool canexecute(object parameter) { return _isenabled; } public event eventhandler canexecutechanged; }
note: need set issyncronizedwithcurrentitem
property on tab control:
<tabcontrol issynchronizedwithcurrentitem="true" />
Comments
Post a Comment