Create reusable CRUD controls in WPF and MVVM

I am creating a WPF Prism MVVM application.

This app will contain many CRUD windows.

I want to optimize the functions (and reduce the amount of code generated) of these windows.

I already used an approach in which I created a โ€œhome pageโ€ with default functions and contained a reserved region for โ€œinjectingโ€ various subcontrollers that might belong to certain objects. I am trying to learn how to do this in WPF in this question .

But what I want to know is what is the template for doing this using WPF and MVVM (or control)?

+1
source share
2 answers

Create an interface that inherits all of your ViewModels CRUD, and your shared ViewModel uses an interface to perform CRUD operations

Here is an example of how the interface and classes might look:

// Generic interface public interface IGenericViewModel { bool Add(); bool Save(); bool Delete(); } // Generic CRUD ViewModel public class GenericViewModel { public IGenericViewModel ObjectViewModel { get; set; } public RelayCommand AddCommand { get ; } public RelayCommand SaveCommand { get ; } public RelayCommand DeleteCommand { get ; } void Add() { ObjectViewModel.Add(); } void Save() { ObjectViewModel.Save(); } void Delete() { ObjectViewModel.Delete(); } } // Specific object ViewModel used by generic CRUD ViewModel public class CustomerViewModel : ViewModelBase, IGenericViewModel { bool IGenericViewModel.Add() { // Add logic } bool IGenericViewModel.Save() { // Save logic } bool IGenericViewModel.Delete() { // Delete object } } 
+2
source

Look at this general WPFCrudControl control, it may be useful to you.

Generic WPF CrudControl is implemented based on the MVVM template. This provides a huge performance boost for simple CRUD operations (add, edit, delete, check, sort, search, and search). The control abstracts both the user interface and the business logic, so it requires a relatively minimal coding effort, while maintaining the ability to customize its behavior.

-1
source

Source: https://habr.com/ru/post/912724/


All Articles