[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
  [Expand]View Models
  [Expand]Commands
  [Collapse]Behaviors
    Getting Started
   [Collapse]Predefined Set
     EventToCommand
     KeyToCommand
     FocusBehavior
     ValidationErrorsHostBehavior
     ConfirmationBehavior
     DependencyPropertyBehavior
     EnumItemsSourceBehavior
     CompositeCommandBehavior
     FunctionBindingBehavior
     MethodToCommandBehavior
     CurrentWindowSerializationBehavior
     BarSubItemThemeSelectorBehavior
     BarSplitItemThemeSelectorBehavior
     RibbonGalleryThemeSelectorBehavior
     RibbonGalleryItemThemePaletteSelectorBehavior
     ReadOnlyDependencyPropertyBindingBehavior
    How to: Create a Custom Behavior
  [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)

MethodToCommandBehavior

The MethodToCommandBehavior class is special behavior that allows you to bind a method to a property of the ICommand type.

Expanded Getting Started

Assume that your task is to invoke GridControl methods by clicking the BarButtonItems located in the View.

Below is a code snippet of the View.

The solution is to use the MethodToCommandBehavior. Attach this behavior to a BarButtonItem, then specify the MethodToCommandBehavior properties step by step.

  • The Target property is responsible for the target-object. In the current scenario, the target object is the BarButtonItem. By default, the MethodToCommandBehavior uses the associated object as the Target, so if you attach the behavior to the required object directly, you can skip the Target initialization.
  • The Command property specifies the name of a property that should contain the ICommand object, which created by the MethodToCommandBehavior based on the specified Method, CanExecuteFunction and Args properties. Its default value is "Command".
  • The Source property should contain the source-object. Due to the fact that the source-method is owned by the GridControl, bind the Source property to the GridControl.
  • The Method property contains the source-method name. Therefore, you need to specify the name of the required method here.
  • The CanExecuteFunction property line allows you to specify a Boolean function that determines whether the method specified using the Method property line can be executed in its current state.
  • The MethodToCommandBehavior also provides several properties that are responsible for the source-method parameters: Arg1, Arg2... Arg15. You can specify no more than 15 arguments.

Expanded Arguments Conversion

The MethodToCommandBehavior automatically converts its specified arguments to the parameterized type if possible. In the code above, the "Descending" string will be automatically converted to the ColumnSortOrder parameterized type.

Expanded Declaration via SmartTag

Assign the MethodToCommandBehavior to the corresponding BarButtonItem.

Specify the source-object. For this, bind the Source property to GridControl by using the Binding Editor.

Choose the required source-object's method from the dropdown list.

After the method has been selected, Smart Tag generates property lines for method's arguments. Specify them in order.

Note that Smart Tag generates property lines based on the maximum number of method's arguments. So, there is no need to specify them simultaneously.

Expanded Example

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