c# - Close WPF User Control Popup in MVVM -


i showing user control dialog in wpf-mvvm application. here code.

  var productview = new product();   var productviewmodel = new productviewmodel();   productview.datacontext = productviewmodel;   this.showdialogwindow("add product", productview); 

and showdialogwindow method this

 protected void showdialogwindow(string header, usercontrol control)  {         radwindow window = new radwindow();         window.owner = app.current.mainwindow;         window.windowstartuplocation = system.windows.windowstartuplocation.centerowner;         window.content = control;                   window.header = header;         window.canclose = true;         window.resizemode = resizemode.noresize;         window.showdialog();  } 

now, have button "close" in dialogwindow user control. how can close window using button ?

note: wpf user control not window. want close using mvvm only

consider leveraging messaging parameters pass around data between objects.

you can use eventaggregator or messagebus.

you can pull in messagebus nuget.

you can subscribe , publish using messagebus.instance.

the idea have user controls subscribe events respond to.

note:

i viewmodels. however, think code-smell when adding code user-controls meant have general use regardless of application employing them.

i use publish subscribe pattern complicated class-dependencies:

viewmodel:

    public class viewmodel : viewmodelbase     {         public viewmodel()         {             closecomand = new delegatecommand((obj) =>                 {                     messagebus.instance.publish(messages.request_deployment_settings_closed, null);                 });         } } 

window:

public partial class somewindow : window {     subscription _subscription = new subscription();      public somewindow()     {         initializecomponent();          _subscription.subscribe(messages.request_deployment_settings_closed, obj =>             {                 this.close();             });     } } 

you can leverage bizmonger.patterns messagebus.

messagebus

public class messagebus {     #region singleton     static messagebus _messagebus = null;     private messagebus() { }      public static messagebus instance     {                 {             if (_messagebus == null)             {                 _messagebus = new messagebus();             }              return _messagebus;         }     }     #endregion      #region members     list<observer> _observers = new list<observer>();     list<observer> _onetimeobservers = new list<observer>();     list<observer> _waitingsubscribers = new list<observer>();     list<observer> _waitingunsubscribers = new list<observer>();      int _publishingcount = 0;     #endregion      public void subscribe(string message, action<object> response)     {         subscribe(message, response, _observers);     }      public void subscribefirstpublication(string message, action<object> response)     {         subscribe(message, response, _onetimeobservers);     }      public int unsubscribe(string message, action<object> response)     {         var observers = new list<observer>(_observers.where(o => o.respond == response).tolist());         observers.addrange(_waitingsubscribers.where(o => o.respond == response));         observers.addrange(_onetimeobservers.where(o => o.respond == response));          if (_publishingcount == 0)         {             observers.foreach(o => _observers.remove(o));         }          else         {             _waitingunsubscribers.addrange(observers);         }          return observers.count;     }      public int unsubscribe(string subscription)     {         var observers = new list<observer>(_observers.where(o => o.subscription == subscription).tolist());         observers.addrange(_waitingsubscribers.where(o => o.subscription == subscription));         observers.addrange(_onetimeobservers.where(o => o.subscription == subscription));          if (_publishingcount == 0)         {             observers.foreach(o => _observers.remove(o));         }          else         {             _waitingunsubscribers.addrange(observers);         }          return observers.count;     }      public void publish(string message, object payload)     {         _publishingcount++;          publish(_observers, message, payload);         publish(_onetimeobservers, message, payload);         publish(_waitingsubscribers, message, payload);          _onetimeobservers.removeall(o => o.subscription == message);         _waitingunsubscribers.clear();          _publishingcount--;     }      private void publish(list<observer> observers, string message, object payload)     {         debug.assert(_publishingcount >= 0);          var subscribers = observers.where(o => o.subscription.tolower() == message.tolower());          foreach (var subscriber in subscribers)         {             subscriber.respond(payload);         }     }      public ienumerable<observer> getobservers(string subscription)     {         var observers = new list<observer>(_observers.where(o => o.subscription == subscription));         return observers;     }      public void clear()     {         _observers.clear();         _onetimeobservers.clear();     }      #region helpers     private void subscribe(string message, action<object> response, list<observer> observers)     {         debug.assert(_publishingcount >= 0);          var observer = new observer() { subscription = message, respond = response };          if (_publishingcount == 0)         {             observers.add(observer);         }         else         {             _waitingsubscribers.add(observer);         }     }     #endregion } 

}

subscription

public class subscription {     #region members     list<observer> _observerlist = new list<observer>();     #endregion      public void unsubscribe(string subscription)     {         var observers = _observerlist.where(o => o.subscription == subscription);          foreach (var observer in observers)         {             messagebus.instance.unsubscribe(observer.subscription, observer.respond);         }          _observerlist.where(o => o.subscription == subscription).tolist().foreach(o => _observerlist.remove(o));     }      public void subscribe(string subscription, action<object> response)     {         messagebus.instance.subscribe(subscription, response);         _observerlist.add(new observer() { subscription = subscription, respond = response });     }      public void subscribefirstpublication(string subscription, action<object> response)     {         messagebus.instance.subscribefirstpublication(subscription, response);     } } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -