Note that there are some explanatory texts on larger screens.

plurals
  1. POM-V-VM - Any Examples of using commands in the ViewModel?
    primarykey
    data
    text
    <p>I've been developing a very large LOB app using my flavor of M-V-VM which I call M-V-MC (Model-View-ModelController), which is a kind of a combination between M-V-C and M-V-VM. I had posted <a href="https://stackoverflow.com/questions/322612/what-are-the-most-common-mistakes-made-in-wpf-development#322679">this answer</a> regarding how views get instantiated in M-V-VM to the question "<a href="https://stackoverflow.com/questions/322612/what-are-the-most-common-mistakes-made-in-wpf-development">what-are-the-most-common-mistakes-made-in-wpf-development</a>".</p> <p><a href="https://stackoverflow.com/users/7021/sam">Sam</a> made the following comment regarding my answer:</p> <blockquote> <p>This creates a follow-up-question: how do you create the views? I use RelayCommands to bind actions from the view to the ViewModel, so the view does not even know an action has fired, does not know he should open a new view. Solution: create an event in the VM for the View to subscribe to?</p> </blockquote> <p>When I originally started M-V-VM development I had this notion that EVERYTHING should live in the ViewModel, and have studied a lot of examples from guys like <a href="http://joshsmithonwpf.wordpress.com/" rel="nofollow noreferrer">Josh Smith</a> and <a href="http://karlshifflett.wordpress.com/" rel="nofollow noreferrer">Karl Shifflett</a>. However I have yet to come up with a good example of when a command needs to live in the ViewModel.</p> <p>For instance, let's say I have a ListView that displays Customers, and a button that I click to allow me to edit the currently selected customer. The ListView (View) is bound to a CustomerVM (ViewModel). Clicking the button fires the EditCustomerCommand which opens a popup window which allows me to edit all the properties of the CustomerVM. Where does this EditCustomerCommand live? If it involves opening a window, (UI functionality), shouldn't it be defined in the code-behind of the view? <img src="https://codingcontext.files.wordpress.com/2009/01/commandflow.png" alt="alt text"></p> <p>Does anyone have any examples of when I should define a command in the View versus the ViewModel?</p> <p><a href="https://stackoverflow.com/users/47006/matthew-wright">Matthew Wright</a> states below:</p> <blockquote> <p>New and delete from a list would be good examples. In those cases, a blank record is added or the current record is deleted by the ViewModel. Any action taken by the view should be in response to those events occurring.</p> </blockquote> <p>So if I click the new button, what happens? A new instance of the CustomerVM is created by the Parent ViewModel and added to it's collection right? So how then would my editing screen get opened? The view should create a new instance of the Customer ViewModel, and pass it in to the ParentVM.Add(newlyCreatedVM) method right? </p> <p>Let's say I delete a customer record via the DeleteCommand living on the VM. the VM calls into the business layer and tries to delete the record. It can't so it returns a message to the VM. I want to display this message in dialogbox. How does the view get the message out of the command action? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload