Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Collapse]Build an Application
  [Expand]Choose Application UI
   Data Management Controls
  [Expand]Printing and Exporting
  [Collapse]WinForms MVVM
   [Collapse]Concepts
     Conventions and Attributes
     Fluent API Support
     Data Bindings and Notifications
     Commands
     Services
     Behaviors
     Layer Communication. Messenger
     View Management
   [Expand]Design-time Support
   [Expand]Tutorials
  [Expand]Skins
  [Expand]Localization
  [Expand]Right-to-Left Layout
   Redistribution and Deployment
 [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]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Behaviors

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
17.1 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.

Tip

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
17.1 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
17.1 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:

WithKey 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.
KeyToCommand 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.

How would you rate this topic?​​​​​​​