Building reusable CRUD controls in WPF and MVVM

后端 未结 2 776
长情又很酷
长情又很酷 2021-02-11 07:33

I am building an WPF Prism MVVM application.

This application will contain a lot of CRUD windows.

I want to optimize the features (and reduce the amount of produ

相关标签:
2条回答
  • 2021-02-11 08:24

    Build an interface which all your CRUD ViewModels will inherit from, and have your generic ViewModel use the interface to execute CRUD operations

    Here's 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
        }
    
    }
    
    0 讨论(0)
  • 2021-02-11 08:27

    Look at this generic control WPFCrudControl, may be useful for you.

    A generic WPF CrudControl implemented based on the MVVM pattern. It gives a huge productivity boost for straightforward CRUD operations (Add, Edit, Delete, Validate, Listing with sorting, paging and searching). The control abstracts both the UI and business logic, so it requires relatively minimal coding effort, while keeping it possible to customize its behavior.

    0 讨论(0)
提交回复
热议问题