[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
  Prerequisites
 [Expand]What's Installed
 [Expand].NET Core 3 Support
 [Expand]Common Concepts
 [Collapse]MVVM Framework
  [Collapse]View Models
    BindableBase
    ViewModelBase
    POCO ViewModels
    Interaction of ViewModels
    Passing Data Between ViewModels (ISupportParameter)
    ViewModel relationships (ISupportParentViewModel)
  [Expand]Commands
  [Expand]Behaviors
  [Expand]Services
  [Expand]DXBinding
  [Expand]MIF
   Converters
   ViewLocator
   Messenger
   Data Annotation Attributes
   LayoutTreeHelper
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[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 View Models allow you to:

  • define bindable properties as simple auto-implemented properties;
  • create methods that function as commands at runtime;
  • make properties and methods implement MVVM-specific interfaces.

This provides clean, simple, maintainable, and testable MVVM code.

The POCO View Models are fully compatible with any WPF controls.

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 fully functional ViewModel, create a class instance with the DevExpress.Mvvm.POCO.ViewModelSource.Create method. See the example below.

You can use the ViewModelSource class to create a View Model instance in XAML.

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

Pass Parameters to the ViewModel Constructor

You can pass parameters to the ViewModel's constructor in any of the following approaches.

  • Use lambda expressions. Lambda expressions work slower, because they are not cached and are compiled anew with each method call.

  • Use delegates. This approach works faster than lambda expressions, because the 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 POCO mechanism generates bindable properties for properties that meet all the following requirements:

  1. The property is public and auto-implemented.

    and

  2. The property has the virtual (C#) or Overridable (VB) modifier.

    and

  3. The property has a public getter, and a protected or public setter.
    Tip

    If the property has no setter, you can use the RaisePropertyChanged extension method to explicitly raise the PropertyChanged event for this property. Refer to the GitHub example for more information on how to use the RaisePropertyChanged extension method to explicitly raise the PropertyChanged event.

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

You can use the BindableProperty attribute to:

  • prevent the POCO mechanism from generating a bindable property for a specified property;
  • specify which method should be invoked when a property value is changing or has been changed. This is useful when the method's name does not match the On[PropertyName]Changed and On[PropertyName]Changing convention.

You can use the Fluent API to control POCO ViewModel generation.

Expanded Commands

POCO mechanism generates commands for all public methods without parameters or with a single parameter. You can use the Command attribute or the Fluent API to control the command generation mechanism.

To update an automatically generated command in a POCO View Model, use the RaiseCanExecuteChanged extension method available from the DevExpress.Mvvm.POCO.POCOViewModelExtensions class.

Refer the Commands topic for more information.

Expanded Services

The DevExpress MVVM Framework provides the Services mechanism. The code sample below demonstrates how to access the Message Box service.

Review the Services in POCO objects topic for more information about accessing services.

Expanded View Model Parent-Child Relationships

POCO View Models can relate 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 ViewModel relationships (ISupportParentViewModel) topic contains more information on how to set the parent-child relationship and its advantages.

Expanded Automatic IDataErrorInfo Implementation

The System.ComponentModel.IDataErrorInfo interface is the standard mechanism for data validation in WPF. You can use this interface to provide validation rules for each individual property or for the entire object. The POCO mechanism allows you to automatically implement the System.ComponentModel.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 System.ComponentModel.IDataErrorInfo interface as follows.

The IDataErrorInfoHelper class allows you to get an error based on specified DataAnnotation attributes or Fluent API.

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

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

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