c# - What should go in each MVVM triad? -


OK, suppose I am creating a program that will list the users' contacts in a list box on the left side of the screen. When a user clicks on a contact, a bunch of messages or whatever appears in the main section of the window

Now my question is how should I view MVVMA triads? My two models are: Contacts, and Messages. The contact model has a list of message models.

Each view model object will have a similar model, right?

And what about the scenes? I have a menu view that is the main window, which will have things like menu, toolbar etc. Do I put the listbox in the menu? Where is my confusion with that place; For example, what should be included in Contact Weave? Just one example of contact? So that dumpetplate, control template, context menu, styles etc for that single contact, and then there is a list box among them in the mainview ...?

Thank you.

The heart of MVVM pattern is the model which is the structure of your data then you have contact model and message model which Is related and well set. Now you want to design your interface. If there is a single window (main view) in the UI, then you will need a single visual model so what is ViewModel included?

Let us assume that you want to display only one contact in your main view and no collection. Now your main visual model may only refer to the properties of your contact, which have appeared in the scene.

  Recover contacts from contact c = / * db / /; Contact name = c.Name; // Create these two properties ContactPhone = c.Phone; // It is necessary to accept only these two properties from your view  

Now you can tie these properties to your main body like this

  & Lt; Textbox Text = {Binding Contact Name} /> & Lt ;! - Assume that DataContext is assigned - & gt; & Lt; Textbox text = {binding contactphone} />  

But we must display the collection in our main view. So we define the individual listism as a new scene. We also need a list of contact names and contact phones, so we wrap these two properties in orbit and make a list of them.

(You also need to add message property here)

So personal views have their own view model. Now we integrate these scenes into our main view.

What should be included in the main view model now?

  • List of contact week models - Contact list
  • Selected contact orientedModel - selected communication
  • List of messaging models - Selected contacts mail message

How are these wires done wired?

  • Contact list - bound to view contact list
  • Selected message list - forced to view message list
  • Selected contacts - From contact list The selectedItem Setter is bound to view the selectedContactMessageList according to the selection
  SelectedContactMessageList.Clear (); Foreach (Message Message in SelectedContact.Messages) {SelectedContactMessageList.Add (New MessageView Model (Message)) // Converted the resulting Model from SelectedContact Message to view message  

This is the same. In this way I have understood the MVVM.

And about the point,

Each view object will have the same model, okay?

No! Instead each scene is mapped to a single visual model

Create a view model for each view of your app and only see the properties you have with your view Are in the model.


Comments