i have wpf application using mvvm. have ischecked value bound boolean on model instance on viewmodel. need bind method on viewmodel checked , unchecked events. (this can track unsaved changes , change background give users visual indication of need save. tried:
<checkbox content="enable" margin="5" ischecked="{binding enabled}" checked="{binding schedulechanged}" unchecked="{binding schedulechanged}" />
but 'provide value on 'system.windows.data.binding' threw exception.' error. advice?
here model working with:
public class schedule : iequatable<schedule> { private datetime _scheduledstart; private datetime _scheduledend; private bool _enabled; private string _url; public datetime scheduledstart { { return _scheduledstart; } set { _scheduledstart = value; } } public datetime scheduledend { { return _scheduledend; } set { if(value < scheduledstart) { throw new argumentexception("scheduled end cannot earlier scheduled start."); } else { _scheduledend = value; } } } public bool enabled { { return _enabled; } set { _enabled = value; } } public string url { { return _url; } set { _url = value; } } public bool equals(schedule other) { if(this.scheduledstart == other.scheduledstart && this.scheduledend == other.scheduledend && this.enabled == other.enabled && this.url == other.url) { return true; } else { return false; } } }
my viewmodel contains property has observablecollection. itemscontrol binds collection , generates list. viewmodel sort of knows model instance, wouldn't know one, don't think.
you should able handle in setter enabled...
public class myviewmodel : viewmodelbase { private bool _isdirty; private bool _enabled; public myviewmodel() { savecommand = new relaycommand(save, cansave); } public icommand savecommand { get; } private void save() { //todo: add saving logic } private bool cansave() { return isdirty; } public bool isdirty { { return _isdirty; } private set { if (_isdirty != value) { raisepropertychanged(); } } } public bool enabled { { return _enabled; } set { if (_enabled != value) { _enabled = value; isdirty = true; } //whatever code need raise inotifypropertychanged.propertychanged event raisepropertychanged(); } } }
you're getting binding error because can't bind control event directly method call.
edit: added more complete example.
the example uses mvvm lite framework, approach should work mvvm implementation.
Comments
Post a Comment