Home c# C # wpf mvvm communication ViewModel - Model and ViewModel - ViewModel

C # wpf mvvm communication ViewModel – Model and ViewModel – ViewModel




I have about this view of the window

It consists of 3 regions: File Manager, Workspace and Menu. I need to add a file using the Menu button in File Manager, View window adding is very simple

TextBox Name must check if such a file and border must become red if exists. I have to do it strictly by MVVM

And actually I collided with 2 questions:

As I understand the Add File View Model must say the Add File View whether the file is with the same name, i.e. should be a model that will receive a collection of names and imaging C Add file View and check the similarity. Here you need to take the names with File Manager Model and send to Add File Model, how to do it? You can create a property in File Manager View Model and via Binding as a parameter to raise a command name collection that opens the window for adding a new file but does this approach MVVM?

2 Question about the connection between ViewModel and Model, as far as I know Model can have properties that implement InotifyPropertyChanged and View can do to them binding, but how it turns the model in the View Model, considering Model with the properties that do not implement InotifyPropertyChanged these Properties need to be wrapped in View Model, how should Model be notified of the Model change?

About Prism, EventagGregator I know, I am interested in a solution without a framework

Answer 1

If you wrote across the old manner without MVVM applications, then you will understand. VM is almost the same as Xaml.cs file before in which we wrote the logic of the interface and data. Also here, in VM goes the logic of the interface and some data to simplify. The logic of working with data here already needs to be taken out. That is, as you thought, you can make a list of LIST files directly in VM and to lend your stackpanel to it or what kind of item you are there. And MODEL is already the class of your files and their properties that you will continue to use. It turns out Bindad to List & LT; Files & GT; .

Maybe the terms somewhere I write not quite correct, but it is such logic that guides via MVVM. In particular, the one on Habré. That is, those elements that we go to change in the View We write in ViewModel? And then we transmit on the modified data to other classes / methods for work.

Answer 2

1) In response to 2 questions, I wrote you that ViewModel should provide a collection of objects (OBServableCollection), which are file. For your functionality with the addition of a file, just 1 team that checks whether there is such an element in the collection, if there is, it sets the property you created (Boolean), which bindows on the Border Visibility and is converted from the BOOL in the visibility. And if not, adds a new model in ObservableCollection and it automatically appears in the list. Also do not forget about the Canexecute method. Here everything is very simple, if the value in the empty field, you cannot add a file. Regarding the opening of the new window. Then here the approach is very simple. Only View can create a view. The creation code of the new view you need to write in the view. You can write declaratively, you can in CodeBehind.

2) The model can implement inotifyPropertyChanged. I will say more, it is absolutely normal. As a real life example, I can give you an example any entity from the database. It in terms of MVVM is a model. You do not quite well understand what MODEL and ViewModel is. ViewModel is translated as a representation model, i.e. This is the functionality of the graphical user interface and it is written in it that implements the user interface behavior, and Model is the implementation of the business logic (it is the same functionality of the application). In your case, ViewModel must return the collection of models (files) (the best ObservableCollection), which have a property with a name value, and this collection is binding to some control (for example, ListBox with a redefined element template). And in your picture, which you led, under the state of the model there is much more than a simple change in properties. You are not forgotten that under these three squares, there is due to the 3rble part of the application (there may be 2K files, where, for example, 800 files, this view, 800 is ViewModel, and the remaining 400 are models) and under the status of the model may be due to values Heaps of objects at the same time.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions