mvvm - Dynamic VIew? where to put logic? -
i programming using mvvm pattern.
my view model looks so
class doorsviewmodel { observablecollection<door> doorcollection; }; the door class looks following
class door { string name; bool isopen; }; my view linked viewmodel, , contains longlistselector picture , name of door. want picture dynamic , change depending on state of door( whether it's open or closed ). how implement picture updates dynamically depending on state of door? should done in viewmodel? or should done within view?
this logic should in viewmodel. logic related view or how things displayed should in viewmodel. no logic should in view (.xaml.cs).
you typically use inotifypropertychanged interface notify view has changed. in case want door image changed when state of door changes. in case try this.
class door: inotifypropertychanged { private string _name; private bool _isopen; public uri doorimage { { if (_isopen) return new uri("uri_to_open.png"); return new uri("uri_to_closed.png"); } } public bool isopen { { return _isopen; } set { _isopen = value; raisepropertychanged("isopen"); // important, notifies ui update door image raisepropertychanged("doorimage"); } } private void raisepropertychanged(string propertyname) { var tmp = propertychanged; if (tmp != null) tmp(this, new propertychangedeventargs(propertyname)); } public event propertychangedeventhandler propertychanged; }; note: have encapsulated fields properties.
if image embedded in assebmly, please check out link learn how write uri image.
Comments
Post a Comment