WPF: Where does MVVM stop and code start?

I created a window that has a ListView in order to display a collection of faces. There are also 3 TextBox es that should display the person’s first name and last name, as well as age. Finally, there is a Button to save the new person data entered into these TextBox es.

People are ListView into ListView by implementing MVVM. It works like a charm! In addition, adding new people to the collection by clicking Button is also done through MVVM.

But there are two use cases: I'm not sure if it makes more sense to use commands, i.e. MVVM, or just code. Use cases:

  • When the user selects a person from a ListView , TextBox es should show the person Details.
  • When a user enters character types instead of numbers in a TextBox that displays the person’s age, he or she should be warned that the data entered is incorrect.

The reason why I doubt if I should use MVVM or code-code is because both use cases are only related to view (GUI), i.e. no interactivity with the business logic of the model or application. The source of the ListView element is bound to the ObservableColleciton<Person> collection of faces, and all the data related to the selected person is already passed to the view when the ListView populated with elements. In the second case of use, again, there is no need to go to the ViewModel so that it lights up in the message window about incorrect user input. What about creating a validation callback in the age dependency property of the ViewModel class?

Thanks for all the clarifications.

+7
source share
4 answers

The main motivation of MVVM is the separation of problems, i.e. separate logic from the presentation. What you describe (search and validation) seems more logical to me, so I would put it in the ViewModel (assuming that it cannot be performed with data binding, of course).

  • Keep in mind that browsing is difficult to verify, so if there is a chance that the logic you implement has significant errors that could put it in the viewModel.

  • An alternative (semi-serious, but usually effective) way to decide if something belongs to a model or viewModel, asks itself what happens if you give a view (window, UserControl or something else) to your graphic designer (even if you are not there, pretend that you are doing this). If you have everything in order with the idea that he can put his C # -component [*] hands on the code (and make a mess out of it), this is usually a sign that the code is strictly connected to the presentation and can safely live in the View. In most cases, you move it to the ViewModel.

[*] simply speaking for educational purposes, many designers are more competent than me :-)

+2
source

Text fields can and must be filled out with bindings in XAML when the ListView selection changes, for example:

 <ListView Name="people" .../> <TextBox Text="{Binding ElementName=people, Path=SelectedItem.FirstName}"/> 

Or, to reduce the encoding, put the text fields in your panel with the DataContext set, for example:

 <Grid DataContext="{Binding ElementName=people, Path=SelectedItem}"> <TextBox Text="{Binding Path=FirstName}"/> <TextBox Text="{Binding Path=LastName}"/> </Grid> 

Validation can be included in XAML, but the code that performs the validation is usually implemented in the class. There is a handy abstract class in System.Windows.Controls called ValidationRule that you can use to quickly create a validator. See this blog post for an example.

+6
source

The only time I start throwing code into code files is when I cannot put it in the ViewModel or deeper in the objects graph.

For example, this is your first time, as mentioned above, C. Lawrence Wenham , fully solvable in XAML code. To achieve this effect, there is no need to resort to code back. And this example can be extended to interact with a collection that is not necessarily represented in a control such as listbox. You can write XAML code that responds to changes in the current item in the collection in the ViewModel through a binding.

Your second situation can also be achieved thanks to the dvalidation tools in the ViewModel and data binding to these objects with your XAML. IDataErrorInfo is a great mechanism that is built in precisely for this purpose. Here is a short article demonstrating the simple use of IDataErrorInfo.

Examples of when you need to look into the code are apparently small and far apart. One example that I came across is when a control does not support ICommand, and you cannot attach functionality to a behavioral element, an example of which is a ListBox. But there are some ways around this limitation, as shown in this wonderful SO question . In addition, in case you need to override the rendering in user controls, you need to do this in code or in an inherited class.

Hope this adds a bit more useful information to the answers.

+6
source

You can use a validation rule for your binding. It will not display a message box (although this is probably possible), but you can define an ErrorTemplate that shows an error.

+2
source

All Articles