[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
 [Expand]What's Installed
 [Collapse]Build an Application
  [Expand]Choose Application UI
  [Expand]Printing and Exporting
  [Collapse]WinForms MVVM
     Conventions and Attributes
     Data Bindings and Notifications
     Layer Communication. Messenger
     Fluent API Support
     View Management
     ViewModel Management
   [Expand]Design-time Support
  [Expand]Right-to-Left Layout
   Redistribution and Deployment
   How to: Perform Actions On Application Startup
 [Expand]Controls and Libraries
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[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)


Behaviors add extra functionality to an object without modifying it. For instance, a close button closes a tab or a form and additionally displays a confirmation dialog. You can use behaviors in MVVM applications to accomplish this.

Expanded Confirmation Behavior


MVVM Best Practices Demo The text below has a related example in the DevExpress 'MVVM Best Practices' demo.

Group: API Code Examples

Module: Behaviors

Example: Simple Behaviors

19.2 Demo Center: Launch the demo

A simple confirmation behavior can only be attached to a cancelable event (for example, form closing or edit value changing). To implement custom behavior, define a class that derives from the ConfirmationBehavior class. It contains two virtual string properties for the confirmation message box's title and text. Override these properties to assign your text strings. The class constructor should inherit the base class constructor with a parameter equal to the name of the event that triggers this behavior.


The ConfirmationBehavior class's last virtual property is the boolean Confirm property. You can also override it to specify the related event's cancel conditions.

Use the MvvmContext component's API to attach this behavior to the target UI element.

You can simplify this process by moving the behavior declaration from a separate class to a generic one.

Fluent API is also supported.

Expanded Event-To-Command Behaviors


MVVM Best Practices Demo The text below has a related example in the DevExpress 'MVVM Best Practices' demo.

Group: API Code Examples

Module: Behaviors

Example: Event-To-Command Behavior

19.2 Demo Center: Launch the demo

Event-to-command behaviors fit all the remaining events if a confirmation behavior requires an event that receives CancelEventArgs type arguments. This behavior binds a command to a target UI element, and when this element fires the required event, the command executes. This can be used for:

  • Third-party UI elements that do not implement the ISupportCommandBinding interface and thus cannot be bound using the MvvmContext component's API (the mvvmContext.BindCommand method);
  • DevExpress controls that require extra functionality. For instance, if you need a SimpleButton to do something on the MouseHover event.

Event-to-command behaviors are implemented similarly to confirmation behaviors: you need to define a separate class that derives from the DevExpress EventToCommandBehavior class. In the class constructor, specify the target event name and the command that should be executed on this event.

Attaching event-to-command behaviors is identical to confirmation behaviors.

Using Fluent API allows you to implement event-to-command behaviors without creating separate classes.

Expanded Key-To-Command and Keys-To-Command Behaviors


MVVM Best Practices Demo The text below has a related example in the DevExpress 'MVVM Best Practices' demo.

Group: API Code Examples

Module: Behaviors

Example: Key-To-Command and Keys-To-Command Behaviors

19.2 Demo Center: Launch the demo

These behaviors allow you to execute commands when end-users press specific keyboard keys.

Binding a single keyboard shortcut

The sample ViewModel below defines the "OnAKey" and "OnAltKey" commands that display service-based notifications.

Use the following fluent API methods of the MvvmContext component to bind these commands to related keys:


The first method parameter is a UI element that must be focused when end-users press certain keys. The second parameter is a key combination.


Refers to a command that should be executed.

The code below binds the "OnAKey" and "OnAltKey" commands to "A" and "Alt+A" keys respectively. Pressing both key combinations fires related commands only if the memo edit control currently has focus.

Binding multiple keys to the same command

You want to bind multiple keys to the same command and pass these keys as parameters, use the WithKeys and KeysToCommands methods. Below is the sample KeyAwareViewModel View Model that contains parameterized "OnKey" and "OnKeyArgs" commands. These commands notify users what keys have been pressed.

The code below binds these commands to multiple hotkeys at once.

Expanded Custom Behaviors

You can implement a custom behavior if there is no ready DevExpress behavior for your needs. For instance, the Ctrl+C keyboard combination copies the entire GridView row by default. You can attach your own behavior if you need to copy the value of the selected row cell only.

This behavior uses the custom IClipboardService service that copies text to a clipboard.

After the custom behavior is ready, register the service and call the AttachBehavior method to attach the behavior to the grid view.

The custom behavior now suppresses the default shortcut handler and copies only the selected cell value.

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