c# - Using a Button_Click event to change textbox content using MVVM -
c# - Using a Button_Click event to change textbox content using MVVM -
i don't know if that's right title anyway. i'm switching winforms , trying larn wpf , mvvm methodology.
i have custom class, incident
, used store info incidents occur team must respond to. building view display info in instances of class user, allow user manipulate it. there several pieces of datetime
info need displayed - start
, end
, notification_received
, actions_taken
. need have little button set datetime.now
each associated textbox
alter underlying value of loaded instance of incident
class.
i'm trying figure out how this. winforms, would've set textbox.text
, incident.start
(etc) datetime.now
in same button_click
function, understanding of mvvm i'm not supposed this, instead should bind textbox
vm , update value of vm.
this i'm stuck. i'm pretty sure i'm on how binding, not part alter value of vm button_click
function. please assist?
you're right - view-model should command change, , textbox should update through binding.
in mvvm pattern, code-behind used. instead of button_click
method, need command binding:
<button command="{binding setalldatestonowcommand}"/>
the command executed when button pressed. setalldatestonowcommand
command handler - should icommand
property on view-model:
public icommand setalldatestonowcommand { get; private set; }
i tend utilize relaycommand
mvvm lite toolkit create command handlers, because syntax clean , simple. command handler initialized in view-model's constructor. handler method passed relaycommand
should set properties on selected incident
object:
public yourviewmodel() { this.setalldatestonowcommand = new relaycommand(this.executesetalldatestonowcommand); } ... public void executesetalldatestonowcommand() { this.selectedincident.start = datetime.now; // etc. }
if bindings on textboxes correctly set up, , properties beingness set firing appropriate propertychanged
events, should updated when properties set in command execution method.
however, i'd suggest should have view-model incident
, implements inotifypropertychanged
interface. command outlined above property on view-model. setting, example, start
property on view-model should set property on incident
object view-model (the "model" object), , should raise propertychanged
event. otherwise, incident
class have implement inotifypropertychanged
, , line between model , view-model classes becomes less clear.
c# wpf mvvm
Comments
Post a Comment