wpf - is it possible execute a method in the view when change a property in the view model with MVVM pattern? -


i open dialog in application , close view when property in view model changed.

so thinking in ths way:

1.- in view.axml.cs (code behind) have method named close() execute method close of view.

2.- in view model have property called viewmodelclosing, bool.

3.- view, in way, don't know how, binding property of view model , execute method in code behind when property changed.

is possible that?

thanks.

Álvaro garcía

the simplest , imo best way accept icommand in viewmodel controller.

since viewmodel should not dependent on view or matter viewcontroller, follwoing solution uses dependency injection / inversion of control.

the delegatecommand (aka relaycommand) wrapper icommand

i have kept code minimum focus on solution.

public class viewcontroller {     private view _view;     private viewmodel _viewmodel;      public viewcontroller()     {         icommand closeview = new delegatecommand(m => closeview());         this._view = new view();         this._viewmodel = new viewmodel(closeview);         this._view.datacontext = this._viewmodel;     }      private void closeview()     {         this._view.close();     } }  public class viewmodel {     private bool _viewmodelclosing;      public icommand closeview { get;set;}      public bool viewmodelclosing     {          { return this._viewmodelclosing; }         set         {             if (value != this._viewmodelclosing)             {                 this._viewmodelclosing = value;                 // odd way.                 // better bind button event in view                  // viewmodel.closeview command                  this.closecommand.execute();             }         }     }      public viewmodel(icommand closecommand)     {         this.closeview = closecommand;     } } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -