[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
 [Expand]What's Installed
 [Expand]Common Concepts
 [Collapse]MVVM Framework
    POCO ViewModels
    Interaction of ViewModels
    Passing Data Between ViewModels (ISupportParameter)
    ViewModel relationships (ISupportParentViewModel)
   Data Annotation Attributes
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

POCO ViewModels

POCO (Plain Old CLR Objects) View Models simplify and accelerate the development process. POCO classes allow you to easily define bindable properties and methods without the need to inherit from many different classes (e.g., ViewModelBase) or implement numerous MVVM-specific interfaces, such as INotifyPropertyChanged and ICommand. POCO ViewModels allow you to define bindable properties as simple auto-implemented properties, as well as create methods that will be represented by commands at runtime. This provides clean, simple, maintainable and testable MVVM code. This is a great approach to the MVVM pattern.

All DevExpress WPF Controls, as well as other WPF Controls, are fully compatible with POCO View Models.

Expanded Basics of Generating POCO View Models

A POCO class does not implement an interface, and does not need to be inherited from a base class, such as ViewModelBase or BindableBase. To transform a POCO class into a working ViewModel, it is necessary to create it with the DevExpress.Mvvm.POCO.ViewModelSource class. See the example below.

The ViewModelSource class can also be used to create a View Model instance in XAML.

The ViewModelSource.Create method creates a descendant of the passed ViewModel class using Reflection Emit and returns its instance at runtime. The code below is similar to the one that the ViewModelSource generates based on the LoginViewModel class.

To pass parameters to the ViewModel's constructor, you can use one of the following approaches.

  • While this approach is the most powerful and beautiful, it is also the slowest, because lambda expressions are not comparable, they remain uncached and compile anew with each method call.

  • Since compiled delegate instances can be cached, this is the quickest approach for passing parameters to the ViewModel constructor.

This example demonstrates how to use the POCO mechanism to create view models.

Expanded Bindable Properties

The rule for generating bindable properties is as follows: all public auto-implemented properties with the virtual modifier and a public getter, along with a protected or public setter, become bindable.

You can define functions that are invoked when properties are changed. These function names should use the following formats: On[PropertyName]Changed and On[PropertyName]Changing.

To define a function that does not match the convention, use the BindableProperty attribute.

You can also take advantage of the Fluent API.

Expanded Commands

Commands are generated for all public methods without parameters or with only one parameter. You can control the generation mechanism using the Command attribute or using the Fluent API.

The following topic contains more information about commands: Commands.

Expanded Services

The DevExpress MVVM Framework provides the Services mechanism. The code below is an example of how to gain access to the Message Box service.

Please review the following topic to learn more about accessing services: Services in POCO objects.

Expanded View Model parent-child relationships

POCO View Models can be related to each other with the parent-child relationship. This is achieved with the ISupportParentViewModel interface that is automatically implemented when you create a POCO object with the ViewModelSource class. With this interface, child View Models may access Services registered in the main View Model. The following topic contains more information on how you can set the parent-child relationship and what benefits can be derived using this mechanism: ViewModel relationships (ISupportParentViewModel).

Expanded Automatic IDataErrorInfo Implementation

The IDataErrorInfo interface is the standard mechanism for data validation in WPF. Using this interface, you can provide validation rules for each property in isolation or alternatively, on the entire object. The POCO mechanism allows you to automatically implement the IDataErrorInfo interface based on defined attributes or Fluent API.

To enable this feature, apply the POCOViewModel attribute for your View Model and set the POCOViewModel.ImplementIDataErrorInfo parameter to True.

When the ViewModelSource generates a descendant of a View Model, it implements the IDataErrorInfo interface as follows.

The IDataErrorInfoHelper class provides the capability to get an error based on defined DataAnnotation attributes or Fluent API.

The code example below demonstrates how to use the POCO mechanism to implement the IDataErrorInfo interface.

If you need to extend the default IDataErrorInfo implementation, you can manually implement the IDataErrorInfo interface and use the IDataErrorInfoHelper class.

Is this topic helpful?​​​​​​​